From bb7782fd21b352973effef45a16ecf6ee34abb65 Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Sat, 10 Oct 2026 06:41:03 +0000 Subject: [PATCH] test(graphql-generator): trim watch coverage to focused regressions --- .../graphql-generator/test/Watch.test.ts | 235 +++--------------- 1 file changed, 38 insertions(+), 197 deletions(-) diff --git a/packages/tools/graphql-generator/test/Watch.test.ts b/packages/tools/graphql-generator/test/Watch.test.ts index 22b4507dc0..f7acc799d1 100644 --- a/packages/tools/graphql-generator/test/Watch.test.ts +++ b/packages/tools/graphql-generator/test/Watch.test.ts @@ -1,14 +1,4 @@ -/** - * `graphqlgen --watch` (EFF-1834 points 14 to 17). - * - * The loop tests drive `Watch.run` with injected events under `TestClock` - * against a scratch project under `test/.tmp`. `watch` fakes - * `FileSystem.watch`: an event published for an absolute path reaches every - * subscription on that path or a directory above it, relative to the - * subscribed path as Node reports it. The smoke test runs `graphqlgen --watch` - * with the real `FileSystem.watch`. - */ -import type * as Config from "@effect/graphql-generator/Config" +/** Watch-loop regressions use injected events and TestClock; the CLI smoke test uses real watchers. */ import * as Generator from "@effect/graphql-generator/Generator" import * as Watch from "@effect/graphql-generator/internal/Watch" import * as NodeServices from "@effect/platform-node/NodeServices" @@ -23,27 +13,14 @@ import * as Layer from "effect/Layer" import * as Option from "effect/Option" import * as Path from "effect/Path" import * as PubSub from "effect/PubSub" -import * as Runtime from "effect/Runtime" import * as Stdio from "effect/Stdio" import * as Stream from "effect/Stream" import { TestClock, TestConsole } from "effect/testing" import { fileURLToPath } from "node:url" -const schemaSdl = (name = "String!") => - `scalar Url -scalar Date - -enum Role { - ADMIN - MEMBER -} - -type User { +const schemaSdl = `type User { id: ID! - name: ${name} - avatar: Url - born: Date - role: Role! + name: String! } type Query { @@ -71,7 +48,7 @@ const project = Effect.fnUntraced(function*(documents: Readonly diagnostic.severity === "error"), []) - for (const file of expected.files) { - assert.strictEqual(yield* read(file.path), file.contents, file.path) - } -}) - const liveSleep = (millis: number) => Effect.sleep(millis).pipe(TestClock.withLive) interface Output { @@ -136,20 +104,16 @@ const output: Effect.Effect = Effect.gen(function*() { const cycles = (out: Output): ReadonlyArray => out.stdout.filter((line) => line.startsWith("regenerated ")) -const unmappedWarnings = (out: Output): ReadonlyArray => - out.stderr.filter((line) => line.includes("warning: Custom scalars without a mapping")) - const show = (out: Output): string => `stdout:\n${out.stdout.join("\n")}\nstderr:\n${out.stderr.join("\n")}` /** * Polls the console until `done` holds, failing if the loop exits first. - * With `advance`, each poll also moves the `TestClock` past the debounce. + * Each poll moves the TestClock past the debounce. */ const waitFor = Effect.fnUntraced(function*( fiber: Fiber.Fiber, description: string, - done: (out: Output) => boolean, - options: { readonly advance: boolean } = { advance: true } + done: (out: Output) => boolean ) { for (let attempt = 0; attempt < 200; attempt++) { const exit = fiber.pollUnsafe() @@ -159,7 +123,7 @@ const waitFor = Effect.fnUntraced(function*( } const out = yield* output if (done(out)) return out - if (options.advance) yield* TestClock.adjust("50 millis") + yield* TestClock.adjust("50 millis") yield* liveSleep(25) } return assert.fail(`timed out waiting for ${description}\n${show(yield* output)}`) @@ -168,25 +132,23 @@ const waitFor = Effect.fnUntraced(function*( /** Starts `Watch.run` over a fake `FileSystem.watch` and returns a handle to drive it. */ const start = Effect.fnUntraced(function*(configPath: string) { const path = yield* Path.Path - const events = yield* PubSub.unbounded<{ readonly _tag: FileSystem.WatchEvent["_tag"]; readonly file: string }>() + const events = yield* PubSub.unbounded() const watch = (watched: string): Stream.Stream => Stream.fromPubSub(events).pipe( Stream.map((event): FileSystem.WatchEvent | undefined => { - if (event.file === watched) return { _tag: event._tag, path: path.basename(watched) } - const relative = path.relative(watched, event.file) - return relative.startsWith("..") || path.isAbsolute(relative) ? undefined : { _tag: event._tag, path: relative } + if (event === watched) return { _tag: "Update", path: path.basename(watched) } + const relative = path.relative(watched, event) + return relative.startsWith("..") || path.isAbsolute(relative) ? undefined : { _tag: "Update", path: relative } }), Stream.filter((event): event is FileSystem.WatchEvent => event !== undefined) ) const fiber = yield* Watch.run({ configPath, watch }).pipe(Effect.forkScoped) - const handle = { - fiber, - emit: (tag: FileSystem.WatchEvent["_tag"], file: string) => PubSub.publish(events, { _tag: tag, file }), + return { + emit: (file: string) => PubSub.publish(events, file), waitFor: ( description: string, - done: (out: Output) => boolean, - options?: { readonly advance: boolean } - ) => waitFor(fiber, description, done, options), + done: (out: Output) => boolean + ) => waitFor(fiber, description, done), waitForCycle: (count: number) => waitFor(fiber, `cycle ${count}`, (out) => cycles(out).length >= count).pipe( Effect.map((out) => { @@ -195,54 +157,26 @@ const start = Effect.fnUntraced(function*(configPath: string) { }) ) } - return handle }) describe("graphqlgen --watch", () => { - it.effect("a document edit regenerates only its file, plus the shared module when the types it reaches change", () => + it.effect("a document edit leaves unchanged outputs unwritten", () => Effect.gen(function*() { const p = yield* project({ "src/a.graphql": query("A", "id"), "src/b.graphql": query("B", "id") }) - const outputs = ["src/a.graphql.ts", "src/b.graphql.ts", "schema.graphql.ts"].map(p.file) - const loop = yield* start(p.configPath) - assert.strictEqual(yield* loop.waitForCycle(1), "regenerated 3 files, deleted 0") - yield* assertUpToDate(p.dir) - - yield* backdate(outputs) - yield* write(p.dir, { "src/a.graphql": query("A", "id", "name") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - assert.strictEqual(yield* loop.waitForCycle(2), "regenerated 1 file, deleted 0") - yield* assertUpToDate(p.dir) - assert.notStrictEqual(yield* mtime(p.file("src/a.graphql.ts")), 0) - assert.strictEqual(yield* mtime(p.file("src/b.graphql.ts")), 0, "b.graphql.ts was rewritten") - assert.strictEqual(yield* mtime(p.file("schema.graphql.ts")), 0, "the shared module was rewritten") - - // `role` reaches the `Role` enum, which the shared module now has to hold. - yield* backdate(outputs) - yield* write(p.dir, { "src/a.graphql": query("A", "id", "name", "role") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - assert.strictEqual(yield* loop.waitForCycle(3), "regenerated 2 files, deleted 0") - yield* assertUpToDate(p.dir) - assert.notStrictEqual(yield* mtime(p.file("schema.graphql.ts")), 0) - assert.strictEqual(yield* mtime(p.file("src/b.graphql.ts")), 0, "b.graphql.ts was rewritten") - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - - it.effect("a schema edit is picked up by every output, and only changed bytes are written", () => - Effect.gen(function*() { - const p = yield* project({ "src/a.graphql": query("A", "id", "name"), "src/b.graphql": query("B", "id") }) const loop = yield* start(p.configPath) yield* loop.waitForCycle(1) yield* backdate(["src/a.graphql.ts", "src/b.graphql.ts", "schema.graphql.ts"].map(p.file)) + const before = yield* read(p.file("src/a.graphql.ts")) - yield* write(p.dir, { "schema.graphql": schemaSdl("String") }) - yield* loop.emit("Update", p.file("schema.graphql")) + yield* write(p.dir, { "src/a.graphql": query("A", "id", "name") }) + yield* loop.emit(p.file("src/a.graphql")) assert.strictEqual(yield* loop.waitForCycle(2), "regenerated 1 file, deleted 0") - yield* assertUpToDate(p.dir) - assert.notStrictEqual(yield* mtime(p.file("src/a.graphql.ts")), 0) - assert.strictEqual(yield* mtime(p.file("src/b.graphql.ts")), 0, "b.graphql.ts was rewritten") - assert.strictEqual(yield* mtime(p.file("schema.graphql.ts")), 0, "the shared module was rewritten") + assert.notStrictEqual(yield* read(p.file("src/a.graphql.ts")), before) + assert.strictEqual(yield* mtime(p.file("src/b.graphql.ts")), 0) + assert.strictEqual(yield* mtime(p.file("schema.graphql.ts")), 0) }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - it.effect("an error cycle prints diagnostics and keeps the last good output; the next good edit recovers", () => + it.effect("an error cycle reports diagnostics without replacing the last good output", () => Effect.gen(function*() { const p = yield* project({ "src/a.graphql": query("A", "id") }) const loop = yield* start(p.configPath) @@ -250,146 +184,53 @@ describe("graphqlgen --watch", () => { const good = yield* read(p.file("src/a.graphql.ts")) yield* write(p.dir, { "src/a.graphql": query("A", "nope") }) - yield* loop.emit("Update", p.file("src/a.graphql")) + yield* loop.emit(p.file("src/a.graphql")) const failed = yield* loop.waitFor( "the error diagnostic", (out) => out.stderr.some((line) => line.includes(`src/a.graphql:3:5: error: Cannot query field "nope"`)) ) assert.strictEqual(cycles(failed).length, 1, show(failed)) assert.strictEqual(yield* read(p.file("src/a.graphql.ts")), good) - - yield* write(p.dir, { "src/a.graphql": query("A", "id", "name") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - assert.strictEqual(yield* loop.waitForCycle(2), "regenerated 1 file, deleted 0") - yield* assertUpToDate(p.dir) }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - it.effect("a failing first run keeps watching", () => - Effect.gen(function*() { - const p = yield* project({ "src/a.graphql": query("A", "nope") }) - const loop = yield* start(p.configPath) - yield* loop.waitFor("the error diagnostic", (out) => out.stderr.some((line) => line.includes("error:"))) - assert.isFalse(yield* exists(p.file("src/a.graphql.ts"))) - - yield* write(p.dir, { "src/a.graphql": query("A", "id") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - assert.strictEqual(yield* loop.waitForCycle(1), "regenerated 2 files, deleted 0") - yield* assertUpToDate(p.dir) - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - - it.effect("a config that fails to load at startup exits with code 2", () => - Effect.gen(function*() { - const p = yield* project({ "src/a.graphql": query("A", "id") }) - yield* write(p.dir, { "graphql.config.ts": configSource({ ...config, documents: "src/**/*.graphql" }) }) - - const exit = yield* Effect.exit(Watch.run({ configPath: p.configPath, watch: () => Stream.never })) - assert.isTrue(Exit.isFailure(exit)) - if (Exit.isFailure(exit)) { - assert.strictEqual(Runtime.getErrorExitCode(Cause.squash(exit.cause)), 2, Cause.pretty(exit.cause)) - } - assert.isFalse(yield* exists(p.file("src/a.graphql.ts"))) - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - - it.effect("the unmapped-scalars warning prints only when its set changes", () => - Effect.gen(function*() { - const p = yield* project({ "src/a.graphql": query("A", "avatar"), "src/b.graphql": query("B", "id") }) - const loop = yield* start(p.configPath) - yield* loop.waitFor("the first warning", (out) => cycles(out).length === 1 && unmappedWarnings(out).length === 1) - - yield* write(p.dir, { "src/b.graphql": query("B", "id", "name") }) - yield* loop.emit("Update", p.file("src/b.graphql")) - yield* loop.waitForCycle(2) - const unrelated = yield* output - assert.strictEqual(unmappedWarnings(unrelated).length, 1, show(unrelated)) - - yield* write(p.dir, { "src/a.graphql": query("A", "avatar", "born") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - const changed = yield* loop.waitFor( - "the second warning", - (out) => cycles(out).length === 3 && unmappedWarnings(out).length === 2 - ) - assert.include(unmappedWarnings(changed)[1], "Date") - assert.include(unmappedWarnings(changed)[1], "Url") - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - - it.effect("a config edit that fails to decode keeps the old config; a valid edit takes effect", () => + it.effect("an invalid config edit keeps the previous config active", () => Effect.gen(function*() { const fs = yield* FileSystem.FileSystem const p = yield* project({ "src/a.graphql": query("A", "id") }) const loop = yield* start(p.configPath) yield* loop.waitForCycle(1) + const before = yield* read(p.file("src/a.graphql.ts")) yield* write(p.dir, { "graphql.config.ts": configSource({ ...config, documents: "src/**/*.graphql" }) }) yield* fs.utimes(p.configPath, 1_000, 1_000) - yield* loop.emit("Update", p.configPath) + yield* loop.emit(p.configPath) yield* loop.waitFor("the config error", (out) => out.stderr.some((line) => line.includes("not a valid config"))) - // The documents edit still regenerates under the previous config. yield* write(p.dir, { "src/a.graphql": query("A", "id", "name") }) - yield* loop.emit("Update", p.file("src/a.graphql")) - yield* loop.waitForCycle(2) - yield* assertUpToDate(p.dir) - - const moved = { ...config, shared: "./src/shared.graphql.ts" } - yield* write(p.dir, { "graphql.config.ts": configSource(moved) }) - yield* fs.utimes(p.configPath, 2_000, 2_000) - yield* loop.emit("Update", p.configPath) - yield* loop.waitForCycle(3) - assert.isTrue(yield* exists(p.file("src/shared.graphql.ts"))) - yield* assertUpToDate(p.dir, moved) + yield* loop.emit(p.file("src/a.graphql")) + assert.strictEqual(yield* loop.waitForCycle(2), "regenerated 1 file, deleted 0") + assert.notStrictEqual(yield* read(p.file("src/a.graphql.ts")), before) }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - it.effect("deleting a .graphql file removes its generated output", () => + it.effect("a burst of document events runs one debounced cycle", () => Effect.gen(function*() { - const fs = yield* FileSystem.FileSystem - const p = yield* project({ "src/a.graphql": query("A", "id"), "src/b.graphql": query("B", "id") }) + const p = yield* project({ "src/a.graphql": query("A", "id") }) const loop = yield* start(p.configPath) yield* loop.waitForCycle(1) - assert.isTrue(yield* exists(p.file("src/b.graphql.ts"))) - - yield* fs.remove(p.file("src/b.graphql")) - yield* loop.emit("Remove", p.file("src/b.graphql")) - assert.strictEqual(yield* loop.waitForCycle(2), "regenerated 0 files, deleted 1") - assert.isFalse(yield* exists(p.file("src/b.graphql.ts"))) - assert.isTrue(yield* exists(p.file("src/a.graphql.ts"))) - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) - - it.effect("events for generated or unmatched files are ignored, and a burst of edits runs one cycle", () => - Effect.gen(function*() { - const p = yield* project({ "src/a.graphql": query("A", "id"), "notes.md": "notes\n" }) - const loop = yield* start(p.configPath) - yield* loop.waitForCycle(1) - const quiet = (description: string) => - Effect.gen(function*() { - yield* liveSleep(100) - const out = yield* output - assert.strictEqual(cycles(out).length, 1, `${description}\n${show(out)}`) - }) - - yield* loop.emit("Update", p.file("src/a.graphql.ts")) - yield* loop.emit("Update", p.file("notes.md")) - yield* liveSleep(25) - yield* TestClock.adjust("200 millis") - yield* quiet("an event for a generated or unmatched file ran a cycle") yield* write(p.dir, { "src/a.graphql": query("A", "id", "name") }) - for (const tag of ["Update", "Update", "Update"] as const) { - yield* loop.emit(tag, p.file("src/a.graphql")) + for (let i = 0; i < 3; i++) { + yield* loop.emit(p.file("src/a.graphql")) yield* liveSleep(25) yield* TestClock.adjust("20 millis") } - yield* quiet("a cycle ran within 20 ms of the last event") + yield* liveSleep(100) + assert.strictEqual(cycles(yield* output).length, 1, "a cycle ran within 20 ms of the last event") - yield* TestClock.adjust("100 millis") - yield* loop.waitFor("the debounced cycle", (out) => cycles(out).length >= 2, { advance: false }) + yield* loop.waitForCycle(2) yield* TestClock.adjust("200 millis") yield* liveSleep(100) - const out = yield* output - assert.deepStrictEqual( - cycles(out), - ["regenerated 2 files, deleted 0", "regenerated 1 file, deleted 0"], - show(out) - ) + assert.deepStrictEqual(cycles(yield* output), ["regenerated 2 files, deleted 0", "regenerated 1 file, deleted 0"]) }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) it.live("graphqlgen --watch regenerates after a real .graphql edit", () =>