|
| 1 | +import { tmpdir } from "node:os"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { FileSystem } from "@effect/platform"; |
| 4 | +import { NodeFileSystem } from "@effect/platform-node"; |
| 5 | +import { describe, expect, it } from "@effect/vitest"; |
| 6 | +import * as Effect from "effect/Effect"; |
| 7 | + |
| 8 | +import { |
| 9 | + loadExecutorScopeConfig, |
| 10 | + resolveLocalWorkspaceContext, |
| 11 | +} from "./config"; |
| 12 | +import { |
| 13 | + migrateLegacyExecutorScopeConfigs, |
| 14 | +} from "./config-migrations"; |
| 15 | +import { |
| 16 | + createLocalExecutorRepositoriesEffect, |
| 17 | +} from "./index"; |
| 18 | + |
| 19 | +const resolveMaybeEffect = <T>( |
| 20 | + value: T | Promise<T> | Effect.Effect<T, Error, never>, |
| 21 | +): Effect.Effect<T, Error, never> => |
| 22 | + Effect.isEffect(value) |
| 23 | + ? value |
| 24 | + : value instanceof Promise |
| 25 | + ? Effect.promise(() => value) |
| 26 | + : Effect.succeed(value); |
| 27 | + |
| 28 | +const makeWorkspaceRoot = () => |
| 29 | + FileSystem.FileSystem.pipe( |
| 30 | + Effect.flatMap((fs) => |
| 31 | + fs.makeTempDirectory({ |
| 32 | + directory: tmpdir(), |
| 33 | + prefix: "executor-config-migrations-", |
| 34 | + }) |
| 35 | + ), |
| 36 | + ); |
| 37 | + |
| 38 | +describe("config-migrations", () => { |
| 39 | + it.effect("rewrites legacy source entries into the current config shape", () => |
| 40 | + Effect.gen(function* () { |
| 41 | + const fs = yield* FileSystem.FileSystem; |
| 42 | + const workspaceRoot = yield* makeWorkspaceRoot(); |
| 43 | + const configDirectory = join(workspaceRoot, ".executor"); |
| 44 | + yield* fs.makeDirectory(configDirectory, { recursive: true }); |
| 45 | + yield* fs.writeFileString( |
| 46 | + join(configDirectory, "executor.jsonc"), |
| 47 | + `{ |
| 48 | + "sources": { |
| 49 | + "github": { |
| 50 | + "kind": "openapi", |
| 51 | + "name": "GitHub", |
| 52 | + "connection": { |
| 53 | + "endpoint": "https://api.github.com" |
| 54 | + }, |
| 55 | + "binding": { |
| 56 | + "specUrl": "https://example.com/openapi.json", |
| 57 | + "defaultHeaders": { |
| 58 | + "x-test": "1" |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + "google-calendar": { |
| 63 | + "kind": "google_discovery", |
| 64 | + "name": "Google Calendar", |
| 65 | + "namespace": "google.calendar", |
| 66 | + "connection": { |
| 67 | + "endpoint": "https://calendar-json.googleapis.com/$discovery/rest?version=v3" |
| 68 | + }, |
| 69 | + "binding": { |
| 70 | + "service": "calendar", |
| 71 | + "version": "v3", |
| 72 | + "discoveryUrl": "https://calendar-json.googleapis.com/$discovery/rest?version=v3", |
| 73 | + "defaultHeaders": { |
| 74 | + "x-goog-api-client": "executor" |
| 75 | + }, |
| 76 | + "scopes": [ |
| 77 | + "https://www.googleapis.com/auth/calendar.readonly" |
| 78 | + ] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +`, |
| 84 | + ); |
| 85 | + |
| 86 | + const context = yield* resolveLocalWorkspaceContext({ workspaceRoot }); |
| 87 | + const beforeMigration = yield* Effect.flip(loadExecutorScopeConfig(context)); |
| 88 | + expect(beforeMigration.message).toContain("Invalid executor config"); |
| 89 | + |
| 90 | + const migratedPaths = yield* migrateLegacyExecutorScopeConfigs(context); |
| 91 | + expect(migratedPaths).toContain(context.projectConfigPath); |
| 92 | + expect(yield* fs.exists(`${context.projectConfigPath}.legacy-backup`)).toBe(true); |
| 93 | + |
| 94 | + const loaded = yield* loadExecutorScopeConfig(context); |
| 95 | + const rewritten = yield* fs.readFileString(context.projectConfigPath, "utf8"); |
| 96 | + |
| 97 | + expect(rewritten).not.toContain("\"connection\""); |
| 98 | + expect(rewritten).not.toContain("\"binding\""); |
| 99 | + expect(loaded.config?.sources?.github?.config).toEqual({ |
| 100 | + specUrl: "https://example.com/openapi.json", |
| 101 | + baseUrl: "https://api.github.com", |
| 102 | + auth: { |
| 103 | + kind: "none", |
| 104 | + }, |
| 105 | + defaultHeaders: { |
| 106 | + "x-test": "1", |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(loaded.config?.sources?.["google-calendar"]?.config).toEqual({ |
| 111 | + service: "calendar", |
| 112 | + version: "v3", |
| 113 | + discoveryUrl: "https://calendar-json.googleapis.com/$discovery/rest?version=v3", |
| 114 | + defaultHeaders: { |
| 115 | + "x-goog-api-client": "executor", |
| 116 | + }, |
| 117 | + scopes: ["https://www.googleapis.com/auth/calendar.readonly"], |
| 118 | + auth: { |
| 119 | + kind: "none", |
| 120 | + }, |
| 121 | + }); |
| 122 | + }).pipe(Effect.provide(NodeFileSystem.layer)), |
| 123 | + ); |
| 124 | + |
| 125 | + it.effect("runs startup migrations before the strict config loader", () => |
| 126 | + Effect.gen(function* () { |
| 127 | + const fs = yield* FileSystem.FileSystem; |
| 128 | + const workspaceRoot = yield* makeWorkspaceRoot(); |
| 129 | + const homeConfigPath = join(workspaceRoot, ".executor-home.jsonc"); |
| 130 | + const homeStateDirectory = join(workspaceRoot, ".executor-home-state"); |
| 131 | + const configDirectory = join(workspaceRoot, ".executor"); |
| 132 | + const projectConfigPath = join(configDirectory, "executor.jsonc"); |
| 133 | + yield* fs.makeDirectory(configDirectory, { recursive: true }); |
| 134 | + yield* fs.writeFileString( |
| 135 | + projectConfigPath, |
| 136 | + `{ |
| 137 | + "secrets": { |
| 138 | + "providers": {}, |
| 139 | + "defaults": {} |
| 140 | + }, |
| 141 | + "sources": { |
| 142 | + "github": { |
| 143 | + "kind": "openapi", |
| 144 | + "config": { |
| 145 | + "specUrl": "https://example.com/openapi.json", |
| 146 | + "baseUrl": "https://api.github.com", |
| 147 | + "auth": { |
| 148 | + "kind": "none" |
| 149 | + }, |
| 150 | + "defaultHeaders": null |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | +`, |
| 156 | + ); |
| 157 | + |
| 158 | + const context = yield* resolveLocalWorkspaceContext({ |
| 159 | + workspaceRoot, |
| 160 | + homeConfigPath, |
| 161 | + homeStateDirectory, |
| 162 | + }); |
| 163 | + const beforeStartup = yield* Effect.flip(loadExecutorScopeConfig(context)); |
| 164 | + expect(beforeStartup.message).toContain("Invalid executor config"); |
| 165 | + |
| 166 | + const repositories = yield* createLocalExecutorRepositoriesEffect({ |
| 167 | + workspaceRoot, |
| 168 | + homeConfigPath, |
| 169 | + homeStateDirectory, |
| 170 | + }); |
| 171 | + const rewritten = yield* fs.readFileString(projectConfigPath, "utf8"); |
| 172 | + const loaded = yield* resolveMaybeEffect( |
| 173 | + repositories.workspace.config.load(), |
| 174 | + ); |
| 175 | + |
| 176 | + expect(yield* fs.exists(`${projectConfigPath}.legacy-backup`)).toBe(true); |
| 177 | + expect(rewritten).not.toContain("\"secrets\""); |
| 178 | + expect(loaded.projectConfig?.sources?.github?.config).toEqual({ |
| 179 | + specUrl: "https://example.com/openapi.json", |
| 180 | + baseUrl: "https://api.github.com", |
| 181 | + auth: { |
| 182 | + kind: "none", |
| 183 | + }, |
| 184 | + defaultHeaders: null, |
| 185 | + }); |
| 186 | + }).pipe(Effect.provide(NodeFileSystem.layer)), |
| 187 | + ); |
| 188 | +}); |
0 commit comments