|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | + |
| 3 | +import { expect } from "@effect/vitest"; |
| 4 | +import { Effect } from "effect"; |
| 5 | +import { composePluginApi } from "@executor-js/api/server"; |
| 6 | +import { connectEmulator } from "@executor-js/emulate"; |
| 7 | +import { graphqlHttpPlugin } from "@executor-js/plugin-graphql/api"; |
| 8 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 9 | +import { variable } from "@executor-js/sdk/http-auth"; |
| 10 | + |
| 11 | +import { createEmulatorInstance } from "../src/emulator-instance"; |
| 12 | +import { scenario } from "../src/scenario"; |
| 13 | +import { Api, Browser, Target } from "../src/services"; |
| 14 | + |
| 15 | +const api = composePluginApi([graphqlHttpPlugin()] as const); |
| 16 | +const unique = (prefix: string): string => `${prefix}_${randomBytes(4).toString("hex")}`; |
| 17 | + |
| 18 | +scenario( |
| 19 | + "GraphQL · failed introspection blocks connection creation with an actionable error", |
| 20 | + {}, |
| 21 | + Effect.gen(function* () { |
| 22 | + const target = yield* Target; |
| 23 | + const browser = yield* Browser; |
| 24 | + const { client: makeApiClient } = yield* Api; |
| 25 | + const identity = yield* target.newIdentity(); |
| 26 | + const client = yield* makeApiClient(api, identity); |
| 27 | + const slug = unique("graphql_health"); |
| 28 | + const emulatorBaseUrl = yield* createEmulatorInstance("github", "graphql-health"); |
| 29 | + const emulator = yield* Effect.promise(() => |
| 30 | + connectEmulator({ baseUrl: emulatorBaseUrl, service: "github" }), |
| 31 | + ); |
| 32 | + |
| 33 | + yield* Effect.promise(() => |
| 34 | + emulator.faults.arm({ |
| 35 | + match: { method: "POST", pathPattern: "/graphql" }, |
| 36 | + response: { status: 401, body: { message: "Bad credentials" } }, |
| 37 | + times: 10, |
| 38 | + }), |
| 39 | + ); |
| 40 | + |
| 41 | + yield* client.graphql.addIntegration({ |
| 42 | + payload: { |
| 43 | + endpoint: `${emulatorBaseUrl}/graphql`, |
| 44 | + slug, |
| 45 | + name: "GraphQL health", |
| 46 | + authenticationTemplate: [ |
| 47 | + { |
| 48 | + slug: "header", |
| 49 | + type: "apiKey", |
| 50 | + headers: { Authorization: [variable("token")] }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + yield* Effect.gen(function* () { |
| 57 | + yield* browser.session(identity, async ({ page, step }) => { |
| 58 | + await step("Open the connection flow", async () => { |
| 59 | + await page.goto(`/integrations/${slug}?addAccount=1&owner=org&template=header`, { |
| 60 | + waitUntil: "networkidle", |
| 61 | + }); |
| 62 | + await page.getByRole("heading", { name: /Add connection · GraphQL health/ }).waitFor(); |
| 63 | + }); |
| 64 | + |
| 65 | + await step("Submit a credential rejected during schema introspection", async () => { |
| 66 | + const dialog = page.getByRole("dialog", { |
| 67 | + name: /Add connection · GraphQL health/, |
| 68 | + }); |
| 69 | + await dialog.getByRole("textbox", { name: "Authorization" }).fill("invalid-token"); |
| 70 | + await dialog.getByRole("button", { name: "Continue" }).click(); |
| 71 | + |
| 72 | + const alert = dialog.getByRole("alert"); |
| 73 | + await alert.waitFor(); |
| 74 | + const message = await alert.textContent(); |
| 75 | + expect(message).toContain("The endpoint rejected the credential with HTTP 401."); |
| 76 | + expect(message).toContain("Check the credential and selected authentication method."); |
| 77 | + await dialog.getByText("Step 1 of 2").waitFor(); |
| 78 | + expect( |
| 79 | + await page.getByText("No connections yet").count(), |
| 80 | + "the rejected credential is not saved", |
| 81 | + ).toBe(1); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // The low-level API can still import an existing credential reference |
| 86 | + // without the browser's preflight. This models connections created before |
| 87 | + // the fix and proves their failed tool sync is no longer a silent zero. |
| 88 | + yield* client.connections.create({ |
| 89 | + payload: { |
| 90 | + owner: "org", |
| 91 | + name: ConnectionName.make("legacy"), |
| 92 | + integration: IntegrationSlug.make(slug), |
| 93 | + template: AuthTemplateSlug.make("header"), |
| 94 | + value: "invalid-token", |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + yield* browser.session(identity, async ({ page, step }) => { |
| 99 | + await step("A failed existing connection explains the empty tool catalogue", async () => { |
| 100 | + await page.goto(`/integrations/${slug}?tab=tools`, { waitUntil: "networkidle" }); |
| 101 | + await page.getByText("Connection rejected", { exact: true }).first().waitFor(); |
| 102 | + await page |
| 103 | + .getByText("The endpoint rejected the credential with HTTP 401.", { |
| 104 | + exact: false, |
| 105 | + }) |
| 106 | + .waitFor(); |
| 107 | + await page.getByRole("button", { name: "Check and sync tools" }).waitFor(); |
| 108 | + }); |
| 109 | + |
| 110 | + await step("The account row carries the same actionable health verdict", async () => { |
| 111 | + await page.getByRole("tab", { name: "Accounts" }).click(); |
| 112 | + await page.getByText("Expired", { exact: true }).waitFor(); |
| 113 | + await page |
| 114 | + .getByText("Check the credential and selected authentication method.", { |
| 115 | + exact: false, |
| 116 | + }) |
| 117 | + .waitFor(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }).pipe( |
| 121 | + Effect.ensuring( |
| 122 | + Effect.all( |
| 123 | + [ |
| 124 | + client.integrations |
| 125 | + .remove({ params: { slug: IntegrationSlug.make(slug) } }) |
| 126 | + .pipe(Effect.ignore), |
| 127 | + Effect.promise(() => emulator.faults.clear()).pipe(Effect.ignore), |
| 128 | + ], |
| 129 | + { concurrency: "unbounded" }, |
| 130 | + ), |
| 131 | + ), |
| 132 | + ); |
| 133 | + }), |
| 134 | +); |
0 commit comments