|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodePath from "node:path"; |
| 3 | +import * as NodeURL from "node:url"; |
| 4 | + |
| 5 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | +import { ProviderInstanceId, type ProviderAuthState } from "@t3tools/contracts"; |
| 7 | +import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings"; |
| 8 | +import { makeAcpRegistryAuth } from "@t3tools/provider-acp-registry/testing"; |
| 9 | +import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport"; |
| 10 | +import { describe, expect, it } from "@effect/vitest"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as Option from "effect/Option"; |
| 13 | +import * as Schema from "effect/Schema"; |
| 14 | +import * as Stream from "effect/Stream"; |
| 15 | + |
| 16 | +const instanceId = ProviderInstanceId.make("acpRegistry_mock"); |
| 17 | +const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)); |
| 18 | +const mockAgentPath = NodePath.join(__dirname, "../../scripts/acp-mock-agent.ts"); |
| 19 | +const decodeSettings = Schema.decodeSync(AcpRegistrySettings); |
| 20 | + |
| 21 | +/** Signs in to the mock ACP agent over its real stdio wire, like the provider settings row. */ |
| 22 | +const signIn = (agentEnvironment: Record<string, string>) => |
| 23 | + Effect.gen(function* () { |
| 24 | + const changed: Array<boolean> = []; |
| 25 | + const controller = yield* makeAcpRegistryAuth({ |
| 26 | + instanceId, |
| 27 | + settings: decodeSettings({ agentId: "mock-agent" }), |
| 28 | + cwd: process.cwd(), |
| 29 | + environment: process.env, |
| 30 | + onChanged: (authenticated) => |
| 31 | + Effect.sync(() => { |
| 32 | + changed.push(authenticated); |
| 33 | + }), |
| 34 | + }).pipe( |
| 35 | + Effect.provideService( |
| 36 | + AcpRegistrySupport.AcpRegistryCatalog, |
| 37 | + AcpRegistrySupport.AcpRegistryCatalog.of({ |
| 38 | + search: () => Effect.die("unused search"), |
| 39 | + prepare: () => Effect.die("unused prepare"), |
| 40 | + uninstallManagedBinary: () => Effect.die("unused uninstall"), |
| 41 | + inspect: () => |
| 42 | + Effect.succeed({ |
| 43 | + status: "ready", |
| 44 | + agentId: "mock-agent", |
| 45 | + version: "1.0.0", |
| 46 | + distribution: "npx", |
| 47 | + }), |
| 48 | + resolve: () => |
| 49 | + Effect.succeed({ |
| 50 | + agent: { |
| 51 | + id: "mock-agent", |
| 52 | + name: "Mock Agent", |
| 53 | + version: "1.0.0", |
| 54 | + description: "ACP sign-in test agent", |
| 55 | + distribution: { npx: { package: "[email protected]" } }, |
| 56 | + }, |
| 57 | + distribution: "npx" as const, |
| 58 | + spawn: { |
| 59 | + command: "node", |
| 60 | + args: [mockAgentPath], |
| 61 | + env: { |
| 62 | + ...process.env, |
| 63 | + T3_ACP_AUTH_METHOD_ID: "mock-login", |
| 64 | + T3_ACP_REQUIRE_AUTH: "1", |
| 65 | + ...agentEnvironment, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }), |
| 69 | + }), |
| 70 | + ), |
| 71 | + ); |
| 72 | + const settled = (phase: ProviderAuthState["phase"]) => |
| 73 | + controller.subscribe("owner").pipe( |
| 74 | + Stream.filter((state) => state.phase === phase || state.phase === "failed"), |
| 75 | + Stream.runHead, |
| 76 | + Effect.map(Option.getOrThrow), |
| 77 | + ); |
| 78 | + yield* controller.subscribe("owner").pipe( |
| 79 | + Stream.filter((state) => (state.methods?.length ?? 0) > 0), |
| 80 | + Stream.runHead, |
| 81 | + ); |
| 82 | + yield* controller.start("owner", Effect.void, "mock-login"); |
| 83 | + return { changed, settled }; |
| 84 | + }); |
| 85 | + |
| 86 | +describe("ACP Registry sign-in", () => { |
| 87 | + it.effect("signs in through an agent-handled method and verifies a session", () => |
| 88 | + Effect.gen(function* () { |
| 89 | + const { changed, settled } = yield* signIn({}); |
| 90 | + expect(yield* settled("succeeded")).toMatchObject({ |
| 91 | + phase: "succeeded", |
| 92 | + message: "Sign-in complete.", |
| 93 | + }); |
| 94 | + expect(changed).toEqual([true]); |
| 95 | + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), |
| 96 | + ); |
| 97 | + |
| 98 | + it.effect("shows the agent's reason when its own sign-in fails", () => |
| 99 | + Effect.gen(function* () { |
| 100 | + const { changed, settled } = yield* signIn({ |
| 101 | + T3_ACP_AUTHENTICATE_ERROR_MESSAGE: "Browser sign-in status could not be retrieved.", |
| 102 | + }); |
| 103 | + expect(yield* settled("succeeded")).toMatchObject({ |
| 104 | + phase: "failed", |
| 105 | + message: |
| 106 | + "The ACP agent could not complete sign-in: Browser sign-in status could not be retrieved.", |
| 107 | + }); |
| 108 | + expect(changed).toEqual([]); |
| 109 | + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), |
| 110 | + ); |
| 111 | +}); |
0 commit comments