Rhys Sullivanremove v3 workinged7a3488 months agoHistory
787 lines · 24.6 KB · TypeScript
RawDownload
import { createServer } from "node:http"; import { describe, expect, it } from "@effect/vitest";import { assertTrue } from "@effect/vitest/utils";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";import * as Option from "effect/Option"; import type { AccountId } from "#schema";import {  ExecutionIdSchema,  ExecutionInteractionIdSchema,  SecretMaterialIdSchema,  SourceIdSchema,} from "#schema";import type { ToolPath } from "@executor/codemode-core"; import {  createSqlControlPlaneRuntime,  LiveExecutionManagerService,} from "./index";import { decodeSourceCredentialSelectionContent } from "./source-credential-interactions";import { withControlPlaneClient } from "./test-http-client"; const makeRuntime = Effect.acquireRelease(  createSqlControlPlaneRuntime({ localDataDir: ":memory:" }),  (runtime) => Effect.promise(() => runtime.close()).pipe(Effect.orDie),); type OpenApiSpecServer = {  baseUrl: string;  specUrl: string;  close: () => Promise<void>;}; const makeOpenApiSpecServer = Effect.acquireRelease(  Effect.promise<OpenApiSpecServer>(    () =>      new Promise<OpenApiSpecServer>((resolve, reject) => {        const openApiDocument = JSON.stringify({          openapi: "3.0.3",          info: {            title: "GitHub Test API",            version: "1.0.0",          },          paths: {            "/repos/{owner}/{repo}": {              get: {                operationId: "repos/get-repo",                tags: ["repos"],                summary: "Get a repository",                responses: {                  200: {                    description: "ok",                  },                },              },            },          },        });         const server = createServer((request, response) => {          if (request.url !== "/openapi.json") {            response.statusCode = 404;            response.end();            return;          }           response.statusCode = 200;          response.setHeader("content-type", "application/json");          response.end(openApiDocument);        });         server.once("error", reject);        server.listen(0, "127.0.0.1", () => {          const address = server.address();          if (!address || typeof address === "string") {            reject(new Error("Failed to bind OpenAPI runtime test server"));            return;          }           const baseUrl = `http://127.0.0.1:${address.port}`;          resolve({            baseUrl,            specUrl: `${baseUrl}/openapi.json`,            close: () =>              new Promise<void>((closeResolve, closeReject) => {                server.close((error) => {                  if (error) {                    closeReject(error);                    return;                  }                   closeResolve();                });              }),          });        });      }),  ),  (server) => Effect.promise(() => server.close()).pipe(Effect.orDie),); const expectLeft = <A, E>(effect: Effect.Effect<A, E, never>) =>  Effect.either(effect).pipe(    Effect.flatMap((result) =>      result._tag === "Left"        ? Effect.succeed(result.left)        : Effect.fail(new Error("Expected effect to fail")),    ),  ); describe("control-plane-runtime", () => {  it.scoped("supports full CRUD flow over HTTP API", () =>    Effect.gen(function* () {      const runtime = yield* makeRuntime;      const openApiServer = yield* makeOpenApiSpecServer;       const createOrg = yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.organizations.create({            payload: {              name: "Acme",            },          }),      );      const organizationId = createOrg.id;       const createWorkspace = yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.workspaces.create({            path: { organizationId },            payload: { name: "Primary" },          }),      );      expect(createWorkspace.createdByAccountId).toBe("acc_1");      const workspaceId = createWorkspace.id;       yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.sources.create({            path: { workspaceId },            payload: {              name: "Github",              kind: "openapi",              endpoint: openApiServer.baseUrl,              specUrl: openApiServer.specUrl,              auth: {                kind: "none",              },            },          }),      );       yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.policies.create({            path: { workspaceId },            payload: {              resourceType: "tool_path",              resourcePattern: "source.github.*",              matchType: "glob",              effect: "allow",              approvalMode: "auto",              priority: 50,              enabled: true,            },          }),      );       const listSources = yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.sources.list({            path: { workspaceId },          }),      );      expect(listSources.length).toBe(1);       const listPolicies = yield* withControlPlaneClient(        { runtime, accountId: "acc_1" },        (client) =>          client.policies.list({            path: { workspaceId },          }),      );      expect(listPolicies.length).toBe(1);    }),  );   it.scoped("discovers and connects an OpenAPI source through the HTTP API", () =>    Effect.gen(function* () {      const runtime = yield* makeRuntime;      const openApiServer = yield* makeOpenApiSpecServer;       const organization = yield* withControlPlaneClient(        { runtime, accountId: "acc_discover" },        (client) =>          client.organizations.create({            payload: { name: "Discovery Org" },          }),      );      const workspace = yield* withControlPlaneClient(        { runtime, accountId: "acc_discover" },        (client) =>          client.workspaces.create({            path: { organizationId: organization.id },            payload: { name: "Discovery Workspace" },          }),      );       const discovered = yield* withControlPlaneClient(        { runtime, accountId: "acc_discover" },        (client) =>          client.sources.discover({            payload: {              url: openApiServer.specUrl,            },          }),      );      expect(discovered.detectedKind).toBe("openapi");      expect(discovered.specUrl).toBe(openApiServer.specUrl);      expect(discovered.endpoint).toBe(openApiServer.baseUrl);       const connected = yield* withControlPlaneClient(        { runtime, accountId: "acc_discover" },        (client) =>          client.sources.connect({            path: { workspaceId: workspace.id },            payload: {              kind: "openapi",              endpoint: discovered.endpoint,              specUrl: discovered.specUrl ?? openApiServer.specUrl,              name: discovered.name,              namespace: discovered.namespace,              auth: { kind: "none" },            },          }),      );      expect(connected.kind).toBe("connected");      expect(connected.source.kind).toBe("openapi");      expect(connected.source.status).toBe("connected");      expect(connected.source.specUrl).toBe(openApiServer.specUrl);    }),  );   it.scoped("captures credential requests through the local HTML flow without persisting raw tokens", () =>    Effect.gen(function* () {      const runtime = yield* makeRuntime;      const installation = runtime.localInstallation;      const executionId = ExecutionIdSchema.make("exec_local_credential");      const sourceId = SourceIdSchema.make("src_local_credential");      const interactionSuffix = "executor.sources.add:test";      const interactionId = ExecutionInteractionIdSchema.make(        `${executionId}:${interactionSuffix}`,      );      const now = Date.now();       yield* runtime.persistence.rows.executions.insert({        id: executionId,        workspaceId: installation.workspaceId,        createdByAccountId: installation.accountId,        status: "running",        code: "return await tools.executor.sources.add(...)",        resultJson: null,        errorText: null,        logsJson: null,        startedAt: now,        completedAt: null,        createdAt: now,        updatedAt: now,      });       yield* runtime.persistence.rows.sources.insert({        id: sourceId,        workspaceId: installation.workspaceId,        name: "GitHub",        kind: "openapi",        endpoint: "https://api.github.com",        status: "auth_required",        enabled: true,        namespace: "github",        transport: null,        queryParamsJson: null,        headersJson: null,        specUrl: "https://example.com/github-openapi.yaml",        defaultHeadersJson: null,        sourceHash: null,        sourceDocumentText: null,        lastError: null,        createdAt: now,        updatedAt: now,      });       const interactionFiber = yield* Effect.gen(function* () {        const liveExecutionManager = yield* LiveExecutionManagerService;        const onElicitation = liveExecutionManager.createOnElicitation({          rows: runtime.persistence.rows,          executionId,        });         return yield* onElicitation({          interactionId: interactionSuffix,          path: "executor.sources.add" as ToolPath,          sourceKey: "executor",          args: {            kind: "openapi",            endpoint: "https://api.github.com",            specUrl: "https://example.com/github-openapi.yaml",            name: "GitHub",            workspaceId: installation.workspaceId,            sourceId,          },          elicitation: {            mode: "url",            message: "Open the secure credential page to connect GitHub",            url: `http://127.0.0.1/v1/workspaces/${encodeURIComponent(installation.workspaceId)}/sources/${encodeURIComponent(sourceId)}/credentials?interactionId=${encodeURIComponent(interactionId)}`,            elicitationId: interactionSuffix,          },        });      }).pipe(        Effect.provide(runtime.runtimeLayer),        Effect.fork,      );       yield* Effect.yieldNow();       const pendingInteraction = yield* runtime.persistence.rows.executionInteractions        .getPendingByExecutionId(executionId);      assertTrue(Option.isSome(pendingInteraction));      expect(pendingInteraction.value.id).toBe(interactionId);       const page = yield* withControlPlaneClient(        { runtime },        (client) =>          client.sources.credentialPage({            path: {              workspaceId: installation.workspaceId,              sourceId,            },            urlParams: {              interactionId: pendingInteraction.value.id,            },          }),      );      expect(page).toContain("Configure Source Access");      expect(page).toContain("GitHub");      expect(page).toContain("Continue without auth");       const submittedPage = yield* withControlPlaneClient(        { runtime },        (client) =>          client.sources.credentialSubmit({            path: {              workspaceId: installation.workspaceId,              sourceId,            },            urlParams: {              interactionId: pendingInteraction.value.id,            },            payload: {              action: "submit",              token: "ghp_local_test_token",            },          }),      );      expect(submittedPage).toContain("Credential Stored");       const response = yield* Fiber.join(interactionFiber);      expect(response.action).toBe("accept");      expect(response.content?.authKind).toBe("bearer");       const decodedCredentialSelection = decodeSourceCredentialSelectionContent(        response.content,      );      expect(decodedCredentialSelection.authKind).toBe("bearer");      expect(        decodedCredentialSelection.authKind === "bearer"          ? decodedCredentialSelection.tokenRef.providerId          : null,      ).toBe("postgres");       const tokenSecretMaterialId = SecretMaterialIdSchema.make(        decodedCredentialSelection.authKind === "bearer"          ? decodedCredentialSelection.tokenRef.handle          : "",      );      const storedSecret = yield* runtime.persistence.rows.secretMaterials.getById(        tokenSecretMaterialId,      );      assertTrue(Option.isSome(storedSecret));      expect(storedSecret.value.value).toBe("ghp_local_test_token");       const storedInteraction = yield* runtime.persistence.rows.executionInteractions.getById(        pendingInteraction.value.id,      );      assertTrue(Option.isSome(storedInteraction));      expect(storedInteraction.value.responseJson).toContain("\"authKind\":\"bearer\"");      expect(storedInteraction.value.responseJson).not.toContain("tokenRef");      expect(storedInteraction.value.responseJson).not.toContain("ghp_local_test_token");    }),  );   it.scoped("allows continuing an OpenAPI source credential request without auth", () =>    Effect.gen(function* () {      const runtime = yield* makeRuntime;      const installation = runtime.localInstallation;      const executionId = ExecutionIdSchema.make("exec_local_credential_continue");      const sourceId = SourceIdSchema.make("src_local_credential_continue");      const interactionSuffix = "executor.sources.add:continue";      const interactionId = ExecutionInteractionIdSchema.make(        `${executionId}:${interactionSuffix}`,      );      const now = Date.now();       yield* runtime.persistence.rows.executions.insert({        id: executionId,        workspaceId: installation.workspaceId,        createdByAccountId: installation.accountId,        status: "running",        code: "return await tools.executor.sources.add(...)",        resultJson: null,        errorText: null,        logsJson: null,        startedAt: now,        completedAt: null,        createdAt: now,        updatedAt: now,      });       yield* runtime.persistence.rows.sources.insert({        id: sourceId,        workspaceId: installation.workspaceId,        name: "GitHub",        kind: "openapi",        endpoint: "https://api.github.com",        status: "auth_required",        enabled: true,        namespace: "github",        transport: null,        queryParamsJson: null,        headersJson: null,        specUrl: "https://example.com/github-openapi.yaml",        defaultHeadersJson: null,        sourceHash: null,        sourceDocumentText: null,        lastError: null,        createdAt: now,        updatedAt: now,      });       const interactionFiber = yield* Effect.gen(function* () {        const liveExecutionManager = yield* LiveExecutionManagerService;        const onElicitation = liveExecutionManager.createOnElicitation({          rows: runtime.persistence.rows,          executionId,        });         return yield* onElicitation({          interactionId: interactionSuffix,          path: "executor.sources.add" as ToolPath,          sourceKey: "executor",          args: {            kind: "openapi",            endpoint: "https://api.github.com",            specUrl: "https://example.com/github-openapi.yaml",            name: "GitHub",            workspaceId: installation.workspaceId,            sourceId,          },          elicitation: {            mode: "url",            message: "Open the secure credential page to connect GitHub",            url: `http://127.0.0.1/v1/workspaces/${encodeURIComponent(installation.workspaceId)}/sources/${encodeURIComponent(sourceId)}/credentials?interactionId=${encodeURIComponent(interactionId)}`,            elicitationId: interactionSuffix,          },        });      }).pipe(        Effect.provide(runtime.runtimeLayer),        Effect.fork,      );       yield* Effect.yieldNow();       const pendingInteraction = yield* runtime.persistence.rows.executionInteractions        .getPendingByExecutionId(executionId);      assertTrue(Option.isSome(pendingInteraction));       const submittedPage = yield* withControlPlaneClient(        { runtime },        (client) =>          client.sources.credentialSubmit({            path: {              workspaceId: installation.workspaceId,              sourceId,            },

Lines 1–500 of 787.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t