|
1 | 1 | import { randomBytes } from "node:crypto"; |
| 2 | +import { createServer } from "node:http"; |
2 | 3 |
|
3 | 4 | import { expect } from "@effect/vitest"; |
4 | 5 | import { Effect } from "effect"; |
5 | 6 | import { composePluginApi } from "@executor-js/api/server"; |
6 | | -import { connectEmulator } from "@executor-js/emulate"; |
7 | 7 | import { graphqlHttpPlugin } from "@executor-js/plugin-graphql/api"; |
8 | 8 | import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
9 | 9 | import { variable } from "@executor-js/sdk/http-auth"; |
10 | 10 |
|
11 | | -import { createEmulatorInstance } from "../src/emulator-instance"; |
12 | 11 | import { scenario } from "../src/scenario"; |
13 | 12 | import { Api, Browser, Target } from "../src/services"; |
14 | 13 | import { visit } from "../src/surfaces/browser"; |
15 | 14 |
|
16 | 15 | const api = composePluginApi([graphqlHttpPlugin()] as const); |
17 | 16 | const unique = (prefix: string): string => `${prefix}_${randomBytes(4).toString("hex")}`; |
18 | 17 |
|
| 18 | +const serveRejectingGraphql = () => |
| 19 | + Effect.acquireRelease( |
| 20 | + Effect.callback<{ readonly url: string; readonly close: () => void }>((resume) => { |
| 21 | + const server = createServer((request, response) => { |
| 22 | + if (request.method === "POST" && request.url === "/graphql") { |
| 23 | + response.writeHead(401, { "content-type": "application/json" }); |
| 24 | + response.end(JSON.stringify({ message: "Bad credentials" })); |
| 25 | + return; |
| 26 | + } |
| 27 | + response.writeHead(404, { "content-type": "application/json" }); |
| 28 | + response.end(JSON.stringify({ message: "Not found" })); |
| 29 | + }); |
| 30 | + server.listen(0, "127.0.0.1", () => { |
| 31 | + const address = server.address(); |
| 32 | + const port = typeof address === "object" && address ? address.port : 0; |
| 33 | + resume( |
| 34 | + Effect.succeed({ |
| 35 | + url: `http://127.0.0.1:${port}`, |
| 36 | + close: () => { |
| 37 | + server.close(); |
| 38 | + server.closeAllConnections(); |
| 39 | + }, |
| 40 | + }), |
| 41 | + ); |
| 42 | + }); |
| 43 | + }), |
| 44 | + (server) => Effect.sync(server.close), |
| 45 | + ); |
| 46 | + |
19 | 47 | scenario( |
20 | 48 | "GraphQL · failed introspection blocks connection creation with an actionable error", |
21 | 49 | {}, |
22 | | - Effect.gen(function* () { |
23 | | - const target = yield* Target; |
24 | | - const browser = yield* Browser; |
25 | | - const { client: makeApiClient } = yield* Api; |
26 | | - const identity = yield* target.newIdentity(); |
27 | | - const client = yield* makeApiClient(api, identity); |
28 | | - const slug = unique("graphql_health"); |
29 | | - const emulatorBaseUrl = yield* createEmulatorInstance("github", "graphql-health"); |
30 | | - const emulator = yield* Effect.promise(() => |
31 | | - connectEmulator({ baseUrl: emulatorBaseUrl, service: "github" }), |
32 | | - ); |
| 50 | + Effect.scoped( |
| 51 | + Effect.gen(function* () { |
| 52 | + const target = yield* Target; |
| 53 | + const browser = yield* Browser; |
| 54 | + const { client: makeApiClient } = yield* Api; |
| 55 | + const identity = yield* target.newIdentity(); |
| 56 | + const client = yield* makeApiClient(api, identity); |
| 57 | + const slug = unique("graphql_health"); |
| 58 | + const upstream = yield* serveRejectingGraphql(); |
33 | 59 |
|
34 | | - // A budget, not a count: nothing in this scenario depends on how many |
35 | | - // times the connect flow introspects, and the emulator's answer when the |
36 | | - // budget runs out is not a neutral pass-through — an unauthenticated |
37 | | - // GraphQL POST to the real handler is GitHub-shaped, so it comes back 403 |
38 | | - // "API rate limit exceeded". The UI then honestly reports HTTP 403 and the |
39 | | - // assertion below fails on a message that has nothing to do with the |
40 | | - // product. Arm enough that one connect attempt cannot exhaust it. |
41 | | - yield* Effect.promise(() => |
42 | | - emulator.faults.arm({ |
43 | | - match: { method: "POST", pathPattern: "/graphql" }, |
44 | | - response: { status: 401, body: { message: "Bad credentials" } }, |
45 | | - times: 100, |
46 | | - }), |
47 | | - ); |
| 60 | + yield* client.graphql.addIntegration({ |
| 61 | + payload: { |
| 62 | + endpoint: `${upstream.url}/graphql`, |
| 63 | + slug, |
| 64 | + name: "GraphQL health", |
| 65 | + authenticationTemplate: [ |
| 66 | + { |
| 67 | + slug: "header", |
| 68 | + type: "apiKey", |
| 69 | + headers: { Authorization: [variable("token")] }, |
| 70 | + }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + }); |
48 | 74 |
|
49 | | - yield* client.graphql.addIntegration({ |
50 | | - payload: { |
51 | | - endpoint: `${emulatorBaseUrl}/graphql`, |
52 | | - slug, |
53 | | - name: "GraphQL health", |
54 | | - authenticationTemplate: [ |
55 | | - { |
56 | | - slug: "header", |
57 | | - type: "apiKey", |
58 | | - headers: { Authorization: [variable("token")] }, |
59 | | - }, |
60 | | - ], |
61 | | - }, |
62 | | - }); |
| 75 | + yield* Effect.gen(function* () { |
| 76 | + yield* browser.session(identity, async ({ page, step }) => { |
| 77 | + await step("Open the connection flow", async () => { |
| 78 | + await visit(page, `/integrations/${slug}?addAccount=1&owner=org&template=header`); |
| 79 | + await page.getByRole("heading", { name: /Add connection · GraphQL health/ }).waitFor(); |
| 80 | + }); |
63 | 81 |
|
64 | | - yield* Effect.gen(function* () { |
65 | | - yield* browser.session(identity, async ({ page, step }) => { |
66 | | - await step("Open the connection flow", async () => { |
67 | | - await visit(page, `/integrations/${slug}?addAccount=1&owner=org&template=header`); |
68 | | - await page.getByRole("heading", { name: /Add connection · GraphQL health/ }).waitFor(); |
69 | | - }); |
| 82 | + await step("Submit a credential rejected during schema introspection", async () => { |
| 83 | + const dialog = page.getByRole("dialog", { |
| 84 | + name: /Add connection · GraphQL health/, |
| 85 | + }); |
| 86 | + await dialog.getByRole("textbox", { name: "Authorization" }).fill("invalid-token"); |
| 87 | + await dialog.getByRole("button", { name: "Continue" }).click(); |
70 | 88 |
|
71 | | - await step("Submit a credential rejected during schema introspection", async () => { |
72 | | - const dialog = page.getByRole("dialog", { |
73 | | - name: /Add connection · GraphQL health/, |
| 89 | + const alert = dialog.getByRole("alert"); |
| 90 | + await alert.waitFor(); |
| 91 | + const message = await alert.textContent(); |
| 92 | + expect(message).toContain("The endpoint rejected the credential with HTTP 401."); |
| 93 | + expect(message).toContain("Check the credential and selected authentication method."); |
| 94 | + await dialog.getByText("Step 1 of 2").waitFor(); |
| 95 | + expect( |
| 96 | + await page.getByText("No connections yet").count(), |
| 97 | + "the rejected credential is not saved", |
| 98 | + ).toBe(1); |
74 | 99 | }); |
75 | | - await dialog.getByRole("textbox", { name: "Authorization" }).fill("invalid-token"); |
76 | | - await dialog.getByRole("button", { name: "Continue" }).click(); |
77 | | - |
78 | | - const alert = dialog.getByRole("alert"); |
79 | | - await alert.waitFor(); |
80 | | - const message = await alert.textContent(); |
81 | | - expect(message).toContain("The endpoint rejected the credential with HTTP 401."); |
82 | | - expect(message).toContain("Check the credential and selected authentication method."); |
83 | | - await dialog.getByText("Step 1 of 2").waitFor(); |
84 | | - expect( |
85 | | - await page.getByText("No connections yet").count(), |
86 | | - "the rejected credential is not saved", |
87 | | - ).toBe(1); |
88 | 100 | }); |
89 | | - }); |
90 | | - |
91 | | - // The low-level API can still import an existing credential reference |
92 | | - // without the browser's preflight. This models connections created before |
93 | | - // the fix and proves their failed tool sync is no longer a silent zero. |
94 | | - yield* client.connections.create({ |
95 | | - payload: { |
96 | | - owner: "org", |
97 | | - name: ConnectionName.make("legacy"), |
98 | | - integration: IntegrationSlug.make(slug), |
99 | | - template: AuthTemplateSlug.make("header"), |
100 | | - value: "invalid-token", |
101 | | - }, |
102 | | - }); |
103 | 101 |
|
104 | | - yield* browser.session(identity, async ({ page, step }) => { |
105 | | - await step("A failed existing connection explains the empty tool catalogue", async () => { |
106 | | - await visit(page, `/integrations/${slug}?tab=tools`); |
107 | | - await page.getByText("Connection rejected", { exact: true }).first().waitFor(); |
108 | | - await page |
109 | | - .getByText("The endpoint rejected the credential with HTTP 401.", { |
110 | | - exact: false, |
111 | | - }) |
112 | | - .waitFor(); |
113 | | - await page.getByRole("button", { name: "Check and sync tools" }).waitFor(); |
| 102 | + // The low-level API can still import an existing credential reference |
| 103 | + // without the browser's preflight. This models connections created before |
| 104 | + // the fix and proves their failed tool sync is no longer a silent zero. |
| 105 | + yield* client.connections.create({ |
| 106 | + payload: { |
| 107 | + owner: "org", |
| 108 | + name: ConnectionName.make("legacy"), |
| 109 | + integration: IntegrationSlug.make(slug), |
| 110 | + template: AuthTemplateSlug.make("header"), |
| 111 | + value: "invalid-token", |
| 112 | + }, |
114 | 113 | }); |
115 | 114 |
|
116 | | - await step("The account row carries the same actionable health verdict", async () => { |
117 | | - await page.getByRole("tab", { name: "Accounts" }).click(); |
118 | | - await page.getByText("Expired", { exact: true }).waitFor(); |
119 | | - await page |
120 | | - .getByText("Check the credential and selected authentication method.", { |
121 | | - exact: false, |
122 | | - }) |
123 | | - .waitFor(); |
| 115 | + yield* browser.session(identity, async ({ page, step }) => { |
| 116 | + await step("A failed existing connection explains the empty tool catalogue", async () => { |
| 117 | + await visit(page, `/integrations/${slug}?tab=tools`); |
| 118 | + await page.getByText("Connection rejected", { exact: true }).first().waitFor(); |
| 119 | + await page |
| 120 | + .getByText("The endpoint rejected the credential with HTTP 401.", { |
| 121 | + exact: false, |
| 122 | + }) |
| 123 | + .waitFor(); |
| 124 | + await page.getByRole("button", { name: "Check and sync tools" }).waitFor(); |
| 125 | + }); |
| 126 | + |
| 127 | + await step("The account row carries the same actionable health verdict", async () => { |
| 128 | + await page.getByRole("tab", { name: "Accounts" }).click(); |
| 129 | + await page.getByText("Expired", { exact: true }).waitFor(); |
| 130 | + await page |
| 131 | + .getByText("Check the credential and selected authentication method.", { |
| 132 | + exact: false, |
| 133 | + }) |
| 134 | + .waitFor(); |
| 135 | + }); |
124 | 136 | }); |
125 | | - }); |
126 | | - }).pipe( |
127 | | - Effect.ensuring( |
128 | | - Effect.all( |
129 | | - [ |
130 | | - client.integrations |
131 | | - .remove({ params: { slug: IntegrationSlug.make(slug) } }) |
132 | | - .pipe(Effect.ignore), |
133 | | - Effect.promise(() => emulator.faults.clear()).pipe(Effect.ignore), |
134 | | - ], |
135 | | - { concurrency: "unbounded" }, |
| 137 | + }).pipe( |
| 138 | + Effect.ensuring( |
| 139 | + client.integrations |
| 140 | + .remove({ params: { slug: IntegrationSlug.make(slug) } }) |
| 141 | + .pipe(Effect.ignore), |
136 | 142 | ), |
137 | | - ), |
138 | | - ); |
139 | | - }), |
| 143 | + ); |
| 144 | + }), |
| 145 | + ), |
140 | 146 | ); |
0 commit comments