The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** Generated GitHub modules drive an HTTP client against canned GitHub responses. */ |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as DateTime from "effect/DateTime" |
| 4 | import * as Effect from "effect/Effect" |
| 5 | import { GraphQLClient, GraphQLProtocol } from "effect/graphql" |
| 6 | import * as HttpBody from "effect/http/HttpBody" |
| 7 | import * as HttpClient from "effect/http/HttpClient" |
| 8 | import * as HttpClientResponse from "effect/http/HttpClientResponse" |
| 9 | import * as Layer from "effect/Layer" |
| 10 | import { importGitHub, type UntypedClient } from "./utils/generator.ts" |
| 11 | |
| 12 | interface Sent { |
| 13 | readonly query: string |
| 14 | readonly operationName: string |
| 15 | readonly variables: unknown |
| 16 | } |
| 17 | |
| 18 | /** A GitHub endpoint that records each request body and answers with `data`. */ |
| 19 | const github = (data: unknown) => { |
| 20 | const sent: Array<Sent> = [] |
| 21 | const layer = GraphQLProtocol.layerHttp({ url: "https://api.github.com/graphql" }).pipe( |
| 22 | Layer.provide(Layer.succeed( |
| 23 | HttpClient.HttpClient, |
| 24 | HttpClient.make((request) => |
| 25 | Effect.sync(() => { |
| 26 | const text = request.body instanceof HttpBody.Uint8Array ? request.body.text : undefined |
| 27 | assert(text !== undefined, "expected a JSON request body") |
| 28 | sent.push(JSON.parse(text)) |
| 29 | return HttpClientResponse.fromWeb( |
| 30 | request, |
| 31 | new Response(JSON.stringify({ data }), { headers: { "content-type": "application/json" } }) |
| 32 | ) |
| 33 | }) |
| 34 | ) |
| 35 | )) |
| 36 | ) |
| 37 | return { sent, layer } |
| 38 | } |
| 39 | |
| 40 | describe("Generated GitHub client", () => { |
| 41 | it.effect("RepoIssues encodes its variables and decodes issues, custom scalars and new enum values", () => |
| 42 | Effect.gen(function*() { |
| 43 | const { IssuesGraphQLGroup, RepoIssues } = yield* importGitHub("issues.graphql.ts") |
| 44 | const { layer, sent } = github({ |
| 45 | repository: { |
| 46 | id: "R_kgDOAbc", |
| 47 | nameWithOwner: "Effect-TS/effect", |
| 48 | issues: { |
| 49 | totalCount: 2, |
| 50 | pageInfo: { hasNextPage: true, endCursor: "Y3Vyc29yOjI=" }, |
| 51 | nodes: [ |
| 52 | { |
| 53 | number: 4182, |
| 54 | title: "Heredoc stdin swallows trailing flags", |
| 55 | state: "OPEN", |
| 56 | stateReason: null, |
| 57 | createdAt: "2026-09-30T12:00:00Z", |
| 58 | url: "https://github.com/Effect-TS/effect/issues/4182", |
| 59 | isPinned: true |
| 60 | }, |
| 61 | { |
| 62 | number: 4100, |
| 63 | title: "Old bug", |
| 64 | state: "CLOSED", |
| 65 | stateReason: "RESOLVED_BY_BOT", |
| 66 | createdAt: "2026-08-01T08:30:00Z", |
| 67 | url: "https://github.com/Effect-TS/effect/issues/4100", |
| 68 | isPinned: null |
| 69 | } |
| 70 | ] |
| 71 | } |
| 72 | } |
| 73 | }) |
| 74 | const client: UntypedClient = yield* GraphQLClient.make<never>(IssuesGraphQLGroup).pipe(Effect.provide(layer)) |
| 75 | const result = yield* client.RepoIssues({ |
| 76 | owner: "Effect-TS", |
| 77 | name: "effect", |
| 78 | states: ["OPEN", "CLOSED"], |
| 79 | orderBy: { field: "CREATED_AT", direction: "DESC" } |
| 80 | }) |
| 81 | |
| 82 | assert.deepStrictEqual(sent, [{ |
| 83 | query: RepoIssues.document, |
| 84 | operationName: "RepoIssues", |
| 85 | variables: { |
| 86 | owner: "Effect-TS", |
| 87 | name: "effect", |
| 88 | states: ["OPEN", "CLOSED"], |
| 89 | orderBy: { field: "CREATED_AT", direction: "DESC" } |
| 90 | } |
| 91 | }]) |
| 92 | |
| 93 | const repository = result.repository |
| 94 | assert(repository !== null) |
| 95 | assert.strictEqual(repository.nameWithOwner, "Effect-TS/effect") |
| 96 | assert.deepStrictEqual(repository.issues.pageInfo, { hasNextPage: true, endCursor: "Y3Vyc29yOjI=" }) |
| 97 | const [open, closed] = repository.issues.nodes ?? [] |
| 98 | assert(open != null && closed != null) |
| 99 | assert.strictEqual(open.number, 4182) |
| 100 | assert.strictEqual(open.isPinned, true) |
| 101 | assert.isTrue(DateTime.isDateTime(open.createdAt)) |
| 102 | assert.strictEqual(DateTime.formatIso(open.createdAt), "2026-09-30T12:00:00.000Z") |
| 103 | assert.instanceOf(open.url, URL) |
| 104 | assert.strictEqual(open.url.pathname, "/Effect-TS/effect/issues/4182") |
| 105 | // A state reason added to the schema after generation still decodes. |
| 106 | assert.strictEqual(closed.stateReason, "RESOLVED_BY_BOT") |
| 107 | assert.isNull(closed.isPinned) |
| 108 | })) |
| 109 | }) |