|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Database } from "bun:sqlite"; |
| 3 | +import { migrate } from "drizzle-orm/bun-sqlite/migrator"; |
| 4 | +import { drizzle } from "drizzle-orm/bun-sqlite"; |
| 5 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +import { migrateLegacyConnections } from "./migrate-connections"; |
| 10 | + |
| 11 | +let workDir: string; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + workDir = mkdtempSync(join(tmpdir(), "executor-migrate-connections-")); |
| 15 | +}); |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + rmSync(workDir, { recursive: true, force: true }); |
| 19 | +}); |
| 20 | + |
| 21 | +const columnNames = (db: Database, table: string): ReadonlyArray<string> => |
| 22 | + ( |
| 23 | + db.prepare(`PRAGMA table_info('${table}')`).all() as ReadonlyArray<{ |
| 24 | + readonly name: string; |
| 25 | + }> |
| 26 | + ).map((column) => column.name); |
| 27 | + |
| 28 | +describe("migrateLegacyConnections", () => { |
| 29 | + it("backfills legacy MCP OAuth rows after connection.kind has been dropped", async () => { |
| 30 | + const db = new Database(join(workDir, "data.db")); |
| 31 | + migrate(drizzle(db), { |
| 32 | + migrationsFolder: join(import.meta.dirname, "../../drizzle"), |
| 33 | + }); |
| 34 | + |
| 35 | + expect(columnNames(db, "connection")).not.toContain("kind"); |
| 36 | + |
| 37 | + const now = Date.now(); |
| 38 | + db.prepare( |
| 39 | + "INSERT INTO secret (scope_id, id, name, provider, created_at) VALUES (?, ?, ?, ?, ?)", |
| 40 | + ).run("scope-1", "access-token", "Access token", "keychain", now); |
| 41 | + db.prepare( |
| 42 | + "INSERT INTO secret (scope_id, id, name, provider, created_at) VALUES (?, ?, ?, ?, ?)", |
| 43 | + ).run("scope-1", "refresh-token", "Refresh token", "keychain", now); |
| 44 | + db.prepare( |
| 45 | + "INSERT INTO mcp_source (scope_id, id, name, config, created_at) VALUES (?, ?, ?, ?, ?)", |
| 46 | + ).run( |
| 47 | + "scope-1", |
| 48 | + "remote-mcp", |
| 49 | + "Remote MCP", |
| 50 | + JSON.stringify({ |
| 51 | + transport: "remote", |
| 52 | + endpoint: "https://example.com/mcp", |
| 53 | + auth: { |
| 54 | + kind: "oauth2", |
| 55 | + accessTokenSecretId: "access-token", |
| 56 | + refreshTokenSecretId: "refresh-token", |
| 57 | + tokenType: "Bearer", |
| 58 | + expiresAt: null, |
| 59 | + scope: "read", |
| 60 | + clientInformation: null, |
| 61 | + authorizationServerUrl: null, |
| 62 | + resourceMetadataUrl: null, |
| 63 | + }, |
| 64 | + }), |
| 65 | + now, |
| 66 | + ); |
| 67 | + |
| 68 | + await migrateLegacyConnections(db); |
| 69 | + |
| 70 | + const connection = db |
| 71 | + .prepare( |
| 72 | + "SELECT id, provider, access_token_secret_id, refresh_token_secret_id FROM connection WHERE scope_id = ?", |
| 73 | + ) |
| 74 | + .get("scope-1") as |
| 75 | + | { |
| 76 | + readonly id: string; |
| 77 | + readonly provider: string; |
| 78 | + readonly access_token_secret_id: string; |
| 79 | + readonly refresh_token_secret_id: string | null; |
| 80 | + } |
| 81 | + | undefined; |
| 82 | + expect(connection).toEqual({ |
| 83 | + id: "mcp-oauth2-remote-mcp", |
| 84 | + provider: "mcp:oauth2", |
| 85 | + access_token_secret_id: "access-token", |
| 86 | + refresh_token_secret_id: "refresh-token", |
| 87 | + }); |
| 88 | + |
| 89 | + const source = db |
| 90 | + .prepare("SELECT config FROM mcp_source WHERE scope_id = ? AND id = ?") |
| 91 | + .get("scope-1", "remote-mcp") as { readonly config: string }; |
| 92 | + expect(JSON.parse(source.config).auth).toEqual({ |
| 93 | + kind: "oauth2", |
| 94 | + connectionId: "mcp-oauth2-remote-mcp", |
| 95 | + }); |
| 96 | + |
| 97 | + const ownedSecrets = db |
| 98 | + .prepare( |
| 99 | + "SELECT id, owned_by_connection_id FROM secret WHERE scope_id = ? ORDER BY id", |
| 100 | + ) |
| 101 | + .all("scope-1"); |
| 102 | + expect(ownedSecrets).toEqual([ |
| 103 | + { |
| 104 | + id: "access-token", |
| 105 | + owned_by_connection_id: "mcp-oauth2-remote-mcp", |
| 106 | + }, |
| 107 | + { |
| 108 | + id: "refresh-token", |
| 109 | + owned_by_connection_id: "mcp-oauth2-remote-mcp", |
| 110 | + }, |
| 111 | + ]); |
| 112 | + |
| 113 | + db.close(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments