import Foundation import HelderCore import HelderServices import Testing @Suite struct SchemaServiceTests { /// A Laravel-looking project plus a `bin` folder holding a fake `php` with the given shell body. private func project(php body: String? = nil) throws -> (dir: TempDirectory, bin: URL) { let dir = try TempDirectory() try dir.write("project/artisan", " SchemaService { let php = PHPLocator(herdPath: herd ?? dir.url("no-herd/php").path, locator: ExecutableLocator(directories: [bin.path])) return SchemaService(root: dir.url("project"), php: php, timeout: timeout) } // MARK: files @Test func detectsArtisanAndTheCommand() async throws { let (dir, bin) = try project() let schema = service(dir, bin: bin) #expect(await schema.isLaravelApp()) #expect(!(await schema.hasCommand())) try dir.write("project/" + SchemaPaths.commandFile, "&2 echo " Wrote database/helder/schema.json (1.0 KB)" """) let log = LineLog() let code = await service(dir, bin: bin).runDump(onLine: log.append) #expect(code == 0) let outputs = log.outputs #expect(outputs.count == 3) #expect(outputs.first == "args: artisan helder:schema --no-ansi --no-interaction") // pwd -P resolves /var to /private/var, so compare the tail only. #expect(outputs.dropFirst().first?.hasSuffix("/" + dir.root.lastPathComponent + "/project") == true) #expect(outputs.last == " Wrote database/helder/schema.json (1.0 KB)") #expect(log.errors == ["oops"]) } @Test func theExitCodeComesBack() async throws { let (dir, bin) = try project(php: "echo ' ERROR Connection refused.'\nexit 1") let log = LineLog() #expect(await service(dir, bin: bin).runDump(onLine: log.append) == 1) #expect(log.outputs == [" ERROR Connection refused."]) } @Test func herdsPhpWinsAndItsFolderLeadsThePath() async throws { let (dir, bin) = try project(php: "echo path-php") try script(in: dir, "herd/bin/php", "echo herd\necho \"${PATH%%:*}\"") let log = LineLog() let code = await service(dir, bin: bin, herd: dir.url("herd/bin/php").path).runDump(onLine: log.append) #expect(code == 0) #expect(log.outputs == ["herd", dir.url("herd/bin").path]) } @Test func thePhpOnThePathIsTheFallback() async throws { let (dir, bin) = try project(php: "echo \"${PATH%%:*}\"") let log = LineLog() #expect(await service(dir, bin: bin).runDump(onLine: log.append) == 0) #expect(log.outputs == [bin.path]) } @Test func noPhpIsOneClearErrorLine() async throws { let (dir, bin) = try project() let log = LineLog() let code = await service(dir, bin: bin).runDump(onLine: log.append) #expect(code != 0) #expect(log.errors == ["php not found. Install Laravel Herd or put php on PATH."]) #expect(log.outputs.isEmpty) } @Test func theRunNeverWaitsOnInput() async throws { let (dir, bin) = try project(php: "read answer\necho \"read: [$answer]\"") let log = LineLog() #expect(await service(dir, bin: bin, timeout: 10).runDump(onLine: log.append) == 0) #expect(log.outputs == ["read: []"]) } @Test func aHungRunTimesOut() async throws { let (dir, bin) = try project(php: "echo started\nexec sleep 30") let log = LineLog() let start = Date() let code = await service(dir, bin: bin, timeout: 0.5).runDump(onLine: log.append) #expect(code == -1) #expect(log.errors == ["timed out after 0.5s"]) #expect(Date().timeIntervalSince(start) < 10) } } @Suite struct SchemaLayoutStoreTests { private func withDefaults(_ body: (UserDefaults) throws -> Void) rethrows { let suite = "helder-test-\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite) } try body(defaults) } @Test func positionsRoundTripPerProject() { withDefaults { defaults in let store = UserDefaultsSchemaLayoutStore(defaults: defaults) #expect(store.loadPositions(root: "/a").isEmpty) store.savePositions(["users": SchemaPoint(x: 40, y: 104.5)], root: "/a") #expect(store.loadPositions(root: "/a") == ["users": SchemaPoint(x: 40, y: 104.5)]) #expect(store.loadPositions(root: "/b").isEmpty) #expect(defaults.data(forKey: "helder.schemaLayout:/a") != nil) } } @Test func unreadablePositionsAreEmpty() { withDefaults { defaults in defaults.set(Data("nope".utf8), forKey: SchemaLayout.defaultsKey(root: "/a")) #expect(UserDefaultsSchemaLayoutStore(defaults: defaults).loadPositions(root: "/a").isEmpty) } } }