import { assert, describe, it } from "@effect/vitest"; import { Effect, Schema, Stream } from "effect"; import { GitHubClient } from "../src/GitHubClient.js"; import { GitHubGraphQLError, GraphQLDocument, GraphQLPageInfo, paginateConnection, paginateConnectionStream, } from "../src/GraphQL.js"; import { PageOptions } from "../src/Rest.js"; import type { Reply } from "./fixtures.js"; import { harness } from "./harness.js"; const Labels = GraphQLDocument.make({ name: "repositoryLabels", document: `query repositoryLabels($owner: String!, $repo: String!, $first: Int!, $after: String) { repository(owner: $owner, name: $repo) { labels(first: $first, after: $after) { nodes { name } pageInfo { hasNextPage endCursor } } } }`, response: Schema.Struct({ repository: Schema.Struct({ labels: Schema.Struct({ nodes: Schema.Array(Schema.Struct({ name: Schema.String })), pageInfo: GraphQLPageInfo, }), }), }), })<{ readonly owner: string; readonly repo: string; readonly first: number; readonly after: string | null }>(); const page = (names: ReadonlyArray, endCursor: string | null, hasNextPage: boolean): Reply => ({ status: 200, body: { data: { repository: { labels: { nodes: names.map((name) => ({ name })), pageInfo: { hasNextPage, endCursor } } } }, }, }); const labelsOf = ( data: (typeof Labels)["decode"] extends (raw: unknown) => Effect.Effect ? A : never, ) => data.repository.labels; const walk = (replies: ReadonlyArray, options?: PageOptions) => Effect.gen(function* () { const { script, base } = harness(replies); const value = yield* Effect.provide( Effect.flatMap(GitHubClient, (client) => paginateConnection(client, Labels, { owner: "acme", repo: "widget" }, labelsOf, options), ), base, ); const variables = script.calls.map((call) => JSON.parse(call.body ?? "{}").variables); return { value, script, variables }; }); const walkFailure = (replies: ReadonlyArray) => Effect.gen(function* () { const { base } = harness(replies); return yield* Effect.flip( Effect.provide( Effect.flatMap(GitHubClient, (client) => paginateConnection(client, Labels, { owner: "acme", repo: "widget" }, labelsOf), ), base, ), ); }); describe("paginateConnection", () => { it.effect("walks every page by cursor and collects the nodes in order", () => Effect.gen(function* () { const { value, script, variables } = yield* walk([ page(["a", "b"], "c1", true), page(["c"], "c2", true), page(["d"], "c3", false), ]); assert.deepStrictEqual( value.map((label) => label.name), ["a", "b", "c", "d"], ); assert.strictEqual(script.count(), 3); assert.deepStrictEqual(variables, [ { owner: "acme", repo: "widget", first: 100, after: null }, { owner: "acme", repo: "widget", first: 100, after: "c1" }, { owner: "acme", repo: "widget", first: 100, after: "c2" }, ]); }), ); it.effect("sends perPage as first", () => Effect.gen(function* () { const { variables } = yield* walk([page(["a"], "c1", false)], PageOptions.make({ perPage: 25 })); assert.strictEqual(variables[0]?.first, 25); }), ); it.effect("stops issuing requests at maxPages", () => Effect.gen(function* () { const { value, script } = yield* walk( [page(["a"], "c1", true), page(["b"], "c2", true), page(["c"], "c3", false)], PageOptions.make({ perPage: 1, maxPages: 2 }), ); assert.deepStrictEqual( value.map((label) => label.name), ["a", "b"], ); assert.strictEqual(script.count(), 2); }), ); it.effect("streams the nodes page by page", () => Effect.gen(function* () { const { script, base } = harness([page(["a"], "c1", true), page(["b"], "c2", false)]); const stream = Stream.unwrap( Effect.map(GitHubClient, (client) => paginateConnectionStream(client, Labels, { owner: "acme", repo: "widget" }, labelsOf), ), ); const first = yield* Effect.provide(Stream.runCollect(stream), base); assert.deepStrictEqual( first.map((label) => label.name), ["a", "b"], ); assert.strictEqual(script.count(), 2); }), ); it.effect("fails decode on hasNextPage with no endCursor, instead of ending early", () => Effect.gen(function* () { const error = yield* walkFailure([page(["a"], null, true)]); assert.strictEqual(error._tag, "GitHubGraphQLError"); assert.strictEqual(error.kind, "decode"); assert.include(error.reason, "endCursor"); }), ); it.effect( "fails decode when a page answers the cursor it was asked for, instead of looping", () => Effect.gen(function* () { const error = yield* walkFailure([page(["a"], "c1", true), page(["b"], "c1", true)]); assert.strictEqual(error.kind, "decode"); assert.include(error.reason, "loop"); }), // A broken loop guard would walk forever; the timeout turns that into a failure. 5_000, ); it.effect("fails with GitHub's GraphQL error, classified, on a failed page", () => Effect.gen(function* () { const error = yield* walkFailure([ page(["a"], "c1", true), { status: 200, body: { data: null, errors: [{ type: "NOT_FOUND", message: "gone" }] } }, ]); assert.strictEqual(error.kind, "notFound"); assert.strictEqual(error.operation, "repositoryLabels"); }), ); it.effect("is driven by a double's graphql, with no transport", () => Effect.gen(function* () { const seen: Array = []; const double = GitHubClient.makeTest({ graphql: >(document: GraphQLDocument, variables: V) => { const after = typeof variables.after === "string" ? variables.after : null; seen.push(after); const body = after === null ? { repository: { labels: { nodes: [{ name: "x" }], pageInfo: { hasNextPage: true, endCursor: "k" } } } } : { repository: { labels: { nodes: [{ name: "y" }], pageInfo: { hasNextPage: false, endCursor: null } } }, }; return document .decode(body) .pipe(Effect.mapError((error) => GitHubGraphQLError.decode(document.name, "double", error))); }, }); const value = yield* paginateConnection(double, Labels, { owner: "acme", repo: "widget" }, labelsOf); assert.deepStrictEqual( value.map((label) => label.name), ["x", "y"], ); assert.deepStrictEqual(seen, [null, "k"]); }), ); }); const Preview = GraphQLDocument.make({ name: "previewProbe", document: "query previewProbe { viewer { login } }", response: Schema.Struct({ viewer: Schema.Struct({ login: Schema.String }) }), headers: { Accept: "application/vnd.github.merge-info-preview+json", "X-Probe": "1" }, })>(); describe("GraphQLDocument headers", () => { it.effect("sends a document's headers, lowercased, beside the client's own credential", () => Effect.gen(function* () { const { script, base } = harness([{ status: 200, body: { data: { viewer: { login: "octo" } } } }]); const value = yield* Effect.provide( Effect.flatMap(GitHubClient, (client) => client.graphql(Preview, {})), base, ); assert.strictEqual(value.viewer.login, "octo"); const sent = script.calls[0]?.headers ?? {}; assert.strictEqual(sent.accept, "application/vnd.github.merge-info-preview+json"); assert.strictEqual(sent["x-probe"], "1"); assert.strictEqual(sent.authorization, "token ghs_test"); assert.include(sent["content-type"] ?? "", "application/json"); assert.deepStrictEqual(Preview.headers, { accept: "application/vnd.github.merge-info-preview+json", "x-probe": "1", }); }), ); it.effect("a document without headers sends octokit's default accept", () => Effect.gen(function* () { const { script, base } = harness([page([], null, false)]); yield* Effect.provide( Effect.flatMap(GitHubClient, (client) => client.graphql(Labels, { owner: "acme", repo: "widget", first: 1, after: null }), ), base, ); assert.notInclude(script.calls[0]?.headers.accept ?? "", "preview"); assert.deepStrictEqual(Labels.headers, {}); }), ); it.effect("refuses, when built, a header that would replace the credential or the body's type", () => Effect.sync(() => { for (const header of ["Authorization", "authorization", "Content-Type", "content-length", "Host"]) { assert.throws( () => GraphQLDocument.make({ name: "clobber", document: "query { viewer { login } }", response: Schema.Unknown, headers: { [header]: "x" }, }), RangeError, header.toLowerCase(), ); } }), ); });