|
| 1 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 2 | +import * as Path from "@effect/platform/Path" |
| 3 | +import { NodeContext } from "@effect/platform-node" |
| 4 | +import { describe, expect, it } from "@effect/vitest" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +import type { TemplateConfig } from "../../src/core/domain.js" |
| 8 | +import { applyProjectFiles } from "../../src/usecases/apply.js" |
| 9 | +import { prepareProjectFiles } from "../../src/usecases/actions/prepare-files.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 fs = yield* _(FileSystem.FileSystem) |
| 17 | + const tempDir = yield* _( |
| 18 | + fs.makeTempDirectoryScoped({ |
| 19 | + prefix: "docker-git-apply-config-" |
| 20 | + }) |
| 21 | + ) |
| 22 | + return yield* _(use(tempDir)) |
| 23 | + }) |
| 24 | + ) |
| 25 | + |
| 26 | +const makeTemplateConfig = ( |
| 27 | + root: string, |
| 28 | + outDir: string, |
| 29 | + path: Path.Path, |
| 30 | + targetDir: string |
| 31 | +): TemplateConfig => ({ |
| 32 | + containerName: "dg-test", |
| 33 | + serviceName: "dg-test", |
| 34 | + sshUser: "dev", |
| 35 | + sshPort: 2222, |
| 36 | + repoUrl: "https://github.com/org/repo.git", |
| 37 | + repoRef: "main", |
| 38 | + targetDir, |
| 39 | + volumeName: "dg-test-home", |
| 40 | + dockerGitPath: path.join(root, ".docker-git"), |
| 41 | + authorizedKeysPath: path.join(root, "authorized_keys"), |
| 42 | + envGlobalPath: path.join(root, ".orch/env/global.env"), |
| 43 | + envProjectPath: path.join(outDir, ".orch/env/project.env"), |
| 44 | + codexAuthPath: path.join(root, ".orch/auth/codex"), |
| 45 | + codexSharedAuthPath: path.join(root, ".orch/auth/codex-shared"), |
| 46 | + codexHome: "/home/dev/.codex", |
| 47 | + enableMcpPlaywright: false, |
| 48 | + pnpmVersion: "10.27.0" |
| 49 | +}) |
| 50 | + |
| 51 | +const isRecord = (value: unknown): value is Record<string, unknown> => |
| 52 | + typeof value === "object" && value !== null |
| 53 | + |
| 54 | +const rewriteTargetDirInConfig = (source: string, targetDir: string): string => { |
| 55 | + const parsed: unknown = JSON.parse(source) |
| 56 | + if (!isRecord(parsed)) { |
| 57 | + throw new Error("invalid docker-git.json root") |
| 58 | + } |
| 59 | + const template = parsed["template"] |
| 60 | + if (!isRecord(template)) { |
| 61 | + throw new Error("invalid docker-git.json template") |
| 62 | + } |
| 63 | + const next = { ...parsed, template: { ...template, targetDir } } |
| 64 | + return `${JSON.stringify(next, null, 2)}\n` |
| 65 | +} |
| 66 | + |
| 67 | +describe("applyProjectFiles", () => { |
| 68 | + it.effect("applies updated docker-git.json to managed files in existing project", () => |
| 69 | + withTempDir((root) => |
| 70 | + Effect.gen(function*(_) { |
| 71 | + const fs = yield* _(FileSystem.FileSystem) |
| 72 | + const path = yield* _(Path.Path) |
| 73 | + const outDir = path.join(root, "project") |
| 74 | + const initialTargetDir = "/home/dev/workspaces/org/repo" |
| 75 | + const updatedTargetDir = "/home/dev/workspaces/org/repo-updated" |
| 76 | + const globalConfig = makeTemplateConfig(root, outDir, path, initialTargetDir) |
| 77 | + const projectConfig = makeTemplateConfig(root, outDir, path, initialTargetDir) |
| 78 | + |
| 79 | + yield* _( |
| 80 | + prepareProjectFiles(outDir, root, globalConfig, projectConfig, { |
| 81 | + force: false, |
| 82 | + forceEnv: false |
| 83 | + }) |
| 84 | + ) |
| 85 | + |
| 86 | + const envProjectPath = path.join(outDir, ".orch/env/project.env") |
| 87 | + yield* _(fs.writeFileString(envProjectPath, "# custom env\nCUSTOM_KEY=1\n")) |
| 88 | + |
| 89 | + const configPath = path.join(outDir, "docker-git.json") |
| 90 | + const configBefore = yield* _(fs.readFileString(configPath)) |
| 91 | + yield* _(fs.writeFileString(configPath, rewriteTargetDirInConfig(configBefore, updatedTargetDir))) |
| 92 | + |
| 93 | + const appliedTemplate = yield* _(applyProjectFiles(outDir)) |
| 94 | + expect(appliedTemplate.targetDir).toBe(updatedTargetDir) |
| 95 | + |
| 96 | + const composeAfter = yield* _(fs.readFileString(path.join(outDir, "docker-compose.yml"))) |
| 97 | + expect(composeAfter).toContain(`TARGET_DIR: "${updatedTargetDir}"`) |
| 98 | + |
| 99 | + const dockerfileAfter = yield* _(fs.readFileString(path.join(outDir, "Dockerfile"))) |
| 100 | + expect(dockerfileAfter).toContain(`RUN mkdir -p ${updatedTargetDir}`) |
| 101 | + |
| 102 | + const envAfter = yield* _(fs.readFileString(envProjectPath)) |
| 103 | + expect(envAfter).toContain("CUSTOM_KEY=1") |
| 104 | + }) |
| 105 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 106 | +}) |
0 commit comments