The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * End-to-end checks for generated abstract types, run through a |
| 3 | * `GraphQLClient` over a mock `GraphQLProtocol`: the GitHub timeline (`node` |
| 4 | * on an interface, the `timelineItems` union and a fragment on the `Actor` |
| 5 | * interface from another file), and the subscriptions set (a `Subscription` |
| 6 | * root, a `@oneOf` input, nested interfaces, an enum and recursive input |
| 7 | * objects). |
| 8 | */ |
| 9 | import { assert, describe, it } from "@effect/vitest" |
| 10 | import * as DateTime from "effect/DateTime" |
| 11 | import * as Effect from "effect/Effect" |
| 12 | import { GraphQLClient } from "effect/graphql" |
| 13 | import { GraphQLClientError } from "effect/graphql/GraphQLClientError" |
| 14 | import * as GraphQLProtocol from "effect/graphql/GraphQLProtocol" |
| 15 | import * as Layer from "effect/Layer" |
| 16 | import * as Stream from "effect/Stream" |
| 17 | import { Messages, MessagesGraphQLGroup } from "./generated/subscriptions/messages.graphql.ts" |
| 18 | import { type Room, RoomEvents, RoomsGraphQLGroup } from "./generated/subscriptions/rooms.graphql.ts" |
| 19 | import type { MemberMatch } from "./generated/subscriptions/shared.graphql.ts" |
| 20 | import { importGitHub, type UntypedClient } from "./utils/generator.ts" |
| 21 | |
| 22 | interface Sent { |
| 23 | readonly query: string |
| 24 | readonly operationName: string |
| 25 | readonly variables: unknown |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * A protocol that records each request and answers `execute` with `data` and |
| 30 | * `subscribe` with one event per entry of `events`. |
| 31 | */ |
| 32 | const mock = (options: { readonly data?: unknown; readonly events?: ReadonlyArray<unknown> }) => { |
| 33 | const sent: Array<Sent> = [] |
| 34 | const record = ({ operationName, query, variables }: GraphQLProtocol.GraphQLRequest) => |
| 35 | sent.push({ query, operationName, variables }) |
| 36 | const layer = Layer.succeed(GraphQLProtocol.GraphQLProtocol, { |
| 37 | execute: (request) => |
| 38 | Effect.sync(() => { |
| 39 | record(request) |
| 40 | return { body: { data: options.data } } |
| 41 | }), |
| 42 | subscribe: (request) => |
| 43 | Stream.unwrap(Effect.sync(() => { |
| 44 | record(request) |
| 45 | return Stream.fromIterable((options.events ?? []).map((data) => ({ body: { data } }))) |
| 46 | })) |
| 47 | }) |
| 48 | return { sent, layer } |
| 49 | } |
| 50 | |
| 51 | const reasonTag = (error: unknown) => { |
| 52 | assert.instanceOf(error, GraphQLClientError) |
| 53 | return error.reason._tag |
| 54 | } |
| 55 | |
| 56 | const at = "2026-10-08T10:00:00Z" |
| 57 | |
| 58 | describe("Generated GitHub timeline client", () => { |
| 59 | it.effect("IssueTimeline decodes the selected members, the other buckets and a fragment on an interface", () => |
| 60 | Effect.gen(function*() { |
| 61 | const { IssueTimeline, TimelineGraphQLGroup } = yield* importGitHub("timeline.graphql.ts") |
| 62 | const { layer, sent } = mock({ |
| 63 | data: { |
| 64 | node: { |
| 65 | __typename: "Issue", |
| 66 | number: 4182, |
| 67 | timelineItems: { |
| 68 | nodes: [ |
| 69 | { |
| 70 | __typename: "IssueComment", |
| 71 | id: "IC_1", |
| 72 | body: "Ship it", |
| 73 | createdAt: at, |
| 74 | author: { |
| 75 | __typename: "User", |
| 76 | login: "tim-smart", |
| 77 | avatarUrl: "https://avatars.githubusercontent.com/u/1", |
| 78 | name: "Tim" |
| 79 | } |
| 80 | }, |
| 81 | { |
| 82 | __typename: "IssueComment", |
| 83 | id: "IC_2", |
| 84 | body: "Bumped", |
| 85 | createdAt: at, |
| 86 | author: { |
| 87 | __typename: "Bot", |
| 88 | login: "renovate", |
| 89 | avatarUrl: "https://avatars.githubusercontent.com/u/2" |
| 90 | } |
| 91 | }, |
| 92 | { __typename: "LabeledEvent", createdAt: at, label: { name: "bug", color: "d73a4a" } }, |
| 93 | { |
| 94 | __typename: "ClosedEvent", |
| 95 | createdAt: at, |
| 96 | closer: { __typename: "Commit", oid: "7ff5048db5", pushedDate: null } |
| 97 | }, |
| 98 | { __typename: "ClosedEvent", createdAt: at, closer: { __typename: "ProjectV2" } }, |
| 99 | { __typename: "MentionedEvent" }, |
| 100 | null |
| 101 | ] |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | }) |
| 106 | const client: UntypedClient = yield* GraphQLClient.make<never>(TimelineGraphQLGroup).pipe(Effect.provide(layer)) |
| 107 | const result = yield* client.IssueTimeline({ id: "I_kwDOAbc" }) |
| 108 | |
| 109 | assert.deepStrictEqual(sent, [{ |
| 110 | query: IssueTimeline.document, |
| 111 | operationName: "IssueTimeline", |
| 112 | variables: { id: "I_kwDOAbc" } |
| 113 | }]) |
| 114 | |
| 115 | const node = result.node |
| 116 | assert(node !== null && node.__typename === "Issue") |
| 117 | assert.strictEqual(node.number, 4182) |
| 118 | const [comment, botComment, labeled, closedByCommit, closedByProject, mentioned, missing] = |
| 119 | node.timelineItems.nodes ?? [] |
| 120 | |
| 121 | assert(comment?.__typename === "IssueComment") |
| 122 | assert.strictEqual(comment.body, "Ship it") |
| 123 | assert.strictEqual(DateTime.formatIso(comment.createdAt), "2026-10-08T10:00:00.000Z") |
| 124 | assert(comment.author?.__typename === "User") |
| 125 | assert.strictEqual(comment.author.name, "Tim") |
| 126 | assert.instanceOf(comment.author.avatarUrl, URL) |
| 127 | |
| 128 | assert(botComment?.__typename === "IssueComment") |
| 129 | assert.strictEqual(botComment.author?.__typename, "Bot") |
| 130 | assert.strictEqual(botComment.author?.login, "renovate") |
| 131 | |
| 132 | assert(labeled?.__typename === "LabeledEvent") |
| 133 | assert.deepStrictEqual(labeled.label, { name: "bug", color: "d73a4a" }) |
| 134 | |
| 135 | assert(closedByCommit?.__typename === "ClosedEvent") |
| 136 | assert(closedByCommit.closer?.__typename === "Commit") |
| 137 | assert.strictEqual(closedByCommit.closer.oid, "7ff5048db5") |
| 138 | assert.isNull(closedByCommit.closer.pushedDate) |
| 139 | |
| 140 | assert(closedByProject?.__typename === "ClosedEvent") |
| 141 | assert.deepStrictEqual(closedByProject.closer, { __typename: "ProjectV2" }) |
| 142 | |
| 143 | // A timeline item type the query didn't select lands in the other bucket. |
| 144 | assert.deepStrictEqual(mentioned, { __typename: "MentionedEvent" }) |
| 145 | assert.isNull(missing) |
| 146 | })) |
| 147 | |
| 148 | it.effect("node types the query didn't select, including ones added later, land in the other bucket", () => |
| 149 | Effect.gen(function*() { |
| 150 | const { TimelineGraphQLGroup } = yield* importGitHub("timeline.graphql.ts") |
| 151 | for (const __typename of ["Repository", "AddedLaterNode"]) { |
| 152 | const { layer } = mock({ data: { node: { __typename } } }) |
| 153 | const client: UntypedClient = yield* GraphQLClient.make<never>(TimelineGraphQLGroup).pipe(Effect.provide(layer)) |
| 154 | const result = yield* client.IssueTimeline({ id: "X" }) |
| 155 | assert.deepStrictEqual<unknown>(result, { node: { __typename } }) |
| 156 | } |
| 157 | })) |
| 158 | |
| 159 | it.effect("a malformed selected member fails with a DecodeError instead of falling into the other bucket", () => |
| 160 | Effect.gen(function*() { |
| 161 | const { TimelineGraphQLGroup } = yield* importGitHub("timeline.graphql.ts") |
| 162 | const { layer } = mock({ |
| 163 | data: { |
| 164 | node: { |
| 165 | __typename: "Issue", |
| 166 | number: 1, |
| 167 | timelineItems: { nodes: [{ __typename: "IssueComment", id: 1 }] } |
| 168 | } |
| 169 | } |
| 170 | }) |
| 171 | const client: UntypedClient = yield* GraphQLClient.make<never>(TimelineGraphQLGroup).pipe(Effect.provide(layer)) |
| 172 | const error = yield* client.IssueTimeline({ id: "X" }).pipe(Effect.flip) |
| 173 | assert.strictEqual(reasonTag(error), "DecodeError") |
| 174 | })) |
| 175 | }) |
| 176 | |
| 177 | describe("Generated subscriptions client", () => { |
| 178 | it.effect("a @oneOf input with two keys fails with an EncodeError and nothing is sent", () => |
| 179 | Effect.gen(function*() { |
| 180 | const { layer, sent } = mock({ data: { room: null } }) |
| 181 | const client = yield* GraphQLClient.make(RoomsGraphQLGroup).pipe(Effect.provide(layer)) |
| 182 | const both = { id: "R_1", slug: "general" } as unknown as Room.Variables["by"] |
| 183 | const error = yield* client.Room({ by: both }).pipe(Effect.flip) |
| 184 | assert.strictEqual(reasonTag(error), "EncodeError") |
| 185 | assert.deepStrictEqual(sent, []) |
| 186 | })) |
| 187 | |
| 188 | it.effect("Messages encodes a DateTime nested across the recursive inputs", () => |
| 189 | Effect.gen(function*() { |
| 190 | const { layer, sent } = mock({ data: { messages: [] } }) |
| 191 | const client = yield* GraphQLClient.make(MessagesGraphQLGroup).pipe(Effect.provide(layer)) |
| 192 | yield* client.Messages({ filter: { or: [{ member: { posted: { postedAfter: DateTime.makeUnsafe(at) } } }] } }) |
| 193 | assert.deepStrictEqual(sent, [{ |
| 194 | query: Messages.document, |
| 195 | operationName: "Messages", |
| 196 | variables: { filter: { or: [{ member: { posted: { postedAfter: "2026-10-08T10:00:00.000Z" } } }] } } |
| 197 | }]) |
| 198 | })) |
| 199 | |
| 200 | it.effect("a nested @oneOf input with two keys fails with an EncodeError and nothing is sent", () => |
| 201 | Effect.gen(function*() { |
| 202 | const { layer, sent } = mock({ data: { messages: [] } }) |
| 203 | const client = yield* GraphQLClient.make(MessagesGraphQLGroup).pipe(Effect.provide(layer)) |
| 204 | const both = { id: "U_1", posted: {} } as unknown as MemberMatch |
| 205 | const error = yield* client.Messages({ filter: { member: both } }).pipe(Effect.flip) |
| 206 | assert.strictEqual(reasonTag(error), "EncodeError") |
| 207 | assert.deepStrictEqual(sent, []) |
| 208 | })) |
| 209 | |
| 210 | it.effect("RoomEvents streams events across the nested interfaces, with the other bucket for the rest", () => |
| 211 | Effect.gen(function*() { |
| 212 | const owner = { name: "Ann", role: "OWNER" } |
| 213 | const { layer, sent } = mock({ |
| 214 | events: [ |
| 215 | { events: { __typename: "MessagePosted", id: "E_1", at, member: owner, body: "hello" } }, |
| 216 | { events: { __typename: "MemberJoined", id: "E_2", at, member: { name: "Bo", role: "MODERATOR" } } }, |
| 217 | { events: { __typename: "RoomRenamed", id: "E_3", at, name: "random" } }, |
| 218 | { events: { __typename: "RoomArchived", id: "E_4", at } } |
| 219 | ] |
| 220 | }) |
| 221 | const client = yield* GraphQLClient.make(RoomsGraphQLGroup).pipe(Effect.provide(layer)) |
| 222 | const events = yield* Stream.runCollect(client.RoomEvents({ room: { id: "R_1" } })) |
| 223 | |
| 224 | assert.deepStrictEqual(sent, [{ |
| 225 | query: RoomEvents.document, |
| 226 | operationName: "RoomEvents", |
| 227 | variables: { room: { id: "R_1" } } |
| 228 | }]) |
| 229 | |
| 230 | const [posted, joined, renamed, archived] = events.map((event) => event.events) |
| 231 | assert(posted?.__typename === "MessagePosted") |
| 232 | assert.strictEqual(posted.body, "hello") |
| 233 | assert.deepStrictEqual(posted.member, owner) |
| 234 | // `member` comes from `... on MemberEvent`, an interface MemberJoined implements. |
| 235 | assert(joined?.__typename === "MemberJoined") |
| 236 | assert.strictEqual(joined.member.name, "Bo") |
| 237 | // A role the server added later still decodes. |
| 238 | assert.strictEqual(joined.member.role, "MODERATOR") |
| 239 | assert(renamed?.__typename === "RoomRenamed") |
| 240 | assert.strictEqual(renamed.name, "random") |
| 241 | assert.deepStrictEqual<unknown>(archived, { __typename: "RoomArchived", id: "E_4", at }) |
| 242 | })) |
| 243 | }) |