|
| 1 | +import * as fs from "node:fs" |
| 2 | + |
| 3 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 4 | +import * as Path from "@effect/platform/Path" |
| 5 | +import { NodeContext } from "@effect/platform-node" |
| 6 | +import { describe, expect, it } from "@effect/vitest" |
| 7 | +import { Effect } from "effect" |
| 8 | + |
| 9 | +import { stageBootstrapSnapshot } from "../../src/usecases/shared-volume-seed.js" |
| 10 | + |
| 11 | +const withTempDir = <A, E, R>( |
| 12 | + use: (tempDir: string) => Effect.Effect<A, E, R> |
| 13 | +): Effect.Effect<A, E, R | FileSystem.FileSystem> => |
| 14 | + Effect.scoped( |
| 15 | + Effect.gen(function*(_) { |
| 16 | + const fileSystem = yield* _(FileSystem.FileSystem) |
| 17 | + const tempDir = yield* _( |
| 18 | + fileSystem.makeTempDirectoryScoped({ |
| 19 | + prefix: "docker-git-shared-volume-seed-" |
| 20 | + }) |
| 21 | + ) |
| 22 | + return yield* _(use(tempDir)) |
| 23 | + }) |
| 24 | + ) |
| 25 | + |
| 26 | +describe("stageBootstrapSnapshot", () => { |
| 27 | + it.effect("copies stable Codex auth files and skips transient broken tmp entries", () => |
| 28 | + withTempDir((root) => |
| 29 | + Effect.gen(function*(_) { |
| 30 | + const fileSystem = yield* _(FileSystem.FileSystem) |
| 31 | + const path = yield* _(Path.Path) |
| 32 | + |
| 33 | + const projectDir = path.join(root, "project") |
| 34 | + const stagingDir = path.join(root, "staging") |
| 35 | + const projectCodexDir = path.join(projectDir, ".orch", "auth", "codex") |
| 36 | + const projectCodexLabelDir = path.join(projectCodexDir, "team-a") |
| 37 | + const projectClaudeDir = path.join(projectDir, ".orch", "auth", "claude", "default") |
| 38 | + const sharedCodexDir = path.join(root, ".docker-git", ".orch", "auth", "codex") |
| 39 | + const sharedCodexLabelDir = path.join(sharedCodexDir, "team-a") |
| 40 | + const envDir = path.join(projectDir, ".orch", "env") |
| 41 | + |
| 42 | + yield* _(fileSystem.makeDirectory(projectCodexDir, { recursive: true })) |
| 43 | + yield* _(fileSystem.makeDirectory(projectCodexLabelDir, { recursive: true })) |
| 44 | + yield* _(fileSystem.makeDirectory(projectClaudeDir, { recursive: true })) |
| 45 | + yield* _(fileSystem.makeDirectory(sharedCodexDir, { recursive: true })) |
| 46 | + yield* _(fileSystem.makeDirectory(sharedCodexLabelDir, { recursive: true })) |
| 47 | + yield* _(fileSystem.makeDirectory(envDir, { recursive: true })) |
| 48 | + yield* _(fileSystem.writeFileString(path.join(projectDir, "authorized_keys"), "ssh-ed25519 test\n")) |
| 49 | + yield* _(fileSystem.writeFileString(path.join(envDir, "global.env"), "GITHUB_TOKEN=test\n")) |
| 50 | + yield* _(fileSystem.writeFileString(path.join(envDir, "project.env"), "CODEX_SHARE_AUTH=1\n")) |
| 51 | + yield* _(fileSystem.writeFileString(path.join(projectCodexDir, "auth.json"), "{\"project\":true}\n")) |
| 52 | + yield* _(fileSystem.writeFileString(path.join(projectCodexDir, "config.toml"), "model = \"gpt-5.4\"\n")) |
| 53 | + yield* _(fileSystem.writeFileString(path.join(projectCodexLabelDir, "auth.json"), "{\"label\":\"team-a\"}\n")) |
| 54 | + yield* _(fileSystem.writeFileString(path.join(projectCodexLabelDir, "config.toml"), "model = \"gpt-5.4\"\n")) |
| 55 | + yield* _(fileSystem.writeFileString(path.join(projectClaudeDir, ".oauth-token"), "claude-token\n")) |
| 56 | + yield* _(fileSystem.writeFileString(path.join(sharedCodexDir, "auth.json"), "{\"shared\":true}\n")) |
| 57 | + yield* _(fileSystem.writeFileString(path.join(sharedCodexLabelDir, "auth.json"), "{\"shared\":\"team-a\"}\n")) |
| 58 | + |
| 59 | + yield* _( |
| 60 | + Effect.sync(() => { |
| 61 | + const brokenShimDir = path.join(sharedCodexDir, "tmp", "arg0", "codex-arg0broken") |
| 62 | + fs.mkdirSync(brokenShimDir, { recursive: true }) |
| 63 | + fs.symlinkSync( |
| 64 | + "/usr/local/bun/install/global/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex/codex", |
| 65 | + path.join(brokenShimDir, "apply_patch") |
| 66 | + ) |
| 67 | + fs.writeFileSync(path.join(brokenShimDir, ".lock"), "") |
| 68 | + fs.mkdirSync(path.join(sharedCodexDir, "log"), { recursive: true }) |
| 69 | + fs.writeFileSync(path.join(sharedCodexDir, "log", "codex-login.log"), "transient log\n") |
| 70 | + fs.mkdirSync(path.join(sharedCodexDir, ".image"), { recursive: true }) |
| 71 | + fs.writeFileSync(path.join(sharedCodexDir, ".image", "Dockerfile"), "FROM scratch\n") |
| 72 | + }) |
| 73 | + ) |
| 74 | + |
| 75 | + yield* _( |
| 76 | + stageBootstrapSnapshot(stagingDir, projectDir, { |
| 77 | + volumeName: "dg-test-home", |
| 78 | + authorizedKeysPath: path.join(projectDir, "authorized_keys"), |
| 79 | + envGlobalPath: path.join(projectDir, ".orch", "env", "global.env"), |
| 80 | + envProjectPath: path.join(projectDir, ".orch", "env", "project.env"), |
| 81 | + codexAuthPath: path.join(projectDir, ".orch", "auth", "codex"), |
| 82 | + codexSharedAuthPath: sharedCodexDir |
| 83 | + }) |
| 84 | + ) |
| 85 | + |
| 86 | + expect(yield* _(fileSystem.readFileString(path.join(stagingDir, "project-auth", "codex", "auth.json")))).toBe( |
| 87 | + "{\"project\":true}\n" |
| 88 | + ) |
| 89 | + expect( |
| 90 | + yield* _(fileSystem.readFileString(path.join(stagingDir, "project-auth", "codex", "config.toml"))) |
| 91 | + ).toBe("model = \"gpt-5.4\"\n") |
| 92 | + expect( |
| 93 | + yield* _(fileSystem.readFileString(path.join(stagingDir, "project-auth", "codex", "team-a", "auth.json"))) |
| 94 | + ).toBe("{\"label\":\"team-a\"}\n") |
| 95 | + expect( |
| 96 | + yield* _(fileSystem.readFileString(path.join(stagingDir, "project-auth", "codex", "team-a", "config.toml"))) |
| 97 | + ).toBe("model = \"gpt-5.4\"\n") |
| 98 | + expect(yield* _(fileSystem.readFileString(path.join(stagingDir, "shared-auth", "codex", "auth.json")))).toBe( |
| 99 | + "{\"shared\":true}\n" |
| 100 | + ) |
| 101 | + expect( |
| 102 | + yield* _(fileSystem.readFileString(path.join(stagingDir, "shared-auth", "codex", "team-a", "auth.json"))) |
| 103 | + ).toBe("{\"shared\":\"team-a\"}\n") |
| 104 | + expect( |
| 105 | + yield* _(fileSystem.readFileString(path.join(stagingDir, "project-auth", "claude", "default", ".oauth-token"))) |
| 106 | + ).toBe("claude-token\n") |
| 107 | + |
| 108 | + expect(yield* _(fileSystem.exists(path.join(stagingDir, "shared-auth", "codex", "tmp")))).toBe(false) |
| 109 | + expect(yield* _(fileSystem.exists(path.join(stagingDir, "shared-auth", "codex", "log")))).toBe(false) |
| 110 | + expect(yield* _(fileSystem.exists(path.join(stagingDir, "shared-auth", "codex", ".image")))).toBe(false) |
| 111 | + expect(yield* _(fileSystem.exists(path.join(stagingDir, "project-auth", "codex", "tmp")))).toBe(false) |
| 112 | + }) |
| 113 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 114 | +}) |
0 commit comments