|
| 1 | +import * as Command from "@effect/platform/Command" |
| 2 | +import * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 3 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 4 | +import * as Path from "@effect/platform/Path" |
| 5 | +import { describe, expect, it } from "@effect/vitest" |
| 6 | +import { Effect } from "effect" |
| 7 | +import * as Inspectable from "effect/Inspectable" |
| 8 | +import * as Sink from "effect/Sink" |
| 9 | +import * as Stream from "effect/Stream" |
| 10 | +import * as nodePath from "node:path" |
| 11 | + |
| 12 | +import { runDockerUpIfNeeded } from "../../src/usecases/actions/docker-up.js" |
| 13 | +import type { CreateCommand } from "../../src/core/domain.js" |
| 14 | + |
| 15 | +type RecordedCommand = { |
| 16 | + readonly command: string |
| 17 | + readonly args: ReadonlyArray<string> |
| 18 | +} |
| 19 | + |
| 20 | +const encode = (value: string): Uint8Array => new TextEncoder().encode(value) |
| 21 | + |
| 22 | +const includesArgsInOrder = ( |
| 23 | + args: ReadonlyArray<string>, |
| 24 | + expected: ReadonlyArray<string> |
| 25 | +): boolean => { |
| 26 | + let offset = 0 |
| 27 | + for (const token of expected) { |
| 28 | + const foundAt = args.indexOf(token, offset) |
| 29 | + if (foundAt === -1) { |
| 30 | + return false |
| 31 | + } |
| 32 | + offset = foundAt + 1 |
| 33 | + } |
| 34 | + return true |
| 35 | +} |
| 36 | + |
| 37 | +const isDownWithRemoveOrphans = (command: RecordedCommand): boolean => |
| 38 | + command.command === "docker" && |
| 39 | + includesArgsInOrder(command.args, ["compose", "down", "-v", "--remove-orphans"]) |
| 40 | + |
| 41 | +const isUp = (command: RecordedCommand): boolean => |
| 42 | + command.command === "docker" && |
| 43 | + includesArgsInOrder(command.args, ["compose", "up", "-d", "--build"]) |
| 44 | + |
| 45 | +const isRmContainer = (name: string) => (command: RecordedCommand): boolean => |
| 46 | + command.command === "docker" && includesArgsInOrder(command.args, ["rm", "-f", name]) |
| 47 | + |
| 48 | +const fakePath: Path.Path = { |
| 49 | + join: (...segments) => nodePath.join(...segments), |
| 50 | + resolve: (...segments) => nodePath.resolve(...segments), |
| 51 | + isAbsolute: (value) => nodePath.isAbsolute(value), |
| 52 | + dirname: (value) => nodePath.dirname(value) |
| 53 | +} as Path.Path |
| 54 | + |
| 55 | +const fakeFileSystem: FileSystem.FileSystem = { |
| 56 | + exists: () => Effect.succeed(false) |
| 57 | +} as FileSystem.FileSystem |
| 58 | + |
| 59 | +const makeFakeExecutor = (recorded: Array<RecordedCommand>): CommandExecutor.CommandExecutor => { |
| 60 | + const start = (command: Command.Command): Effect.Effect<CommandExecutor.Process, never> => |
| 61 | + Effect.gen(function*(_) { |
| 62 | + const flattened = Command.flatten(command) |
| 63 | + for (const entry of flattened) { |
| 64 | + recorded.push({ command: entry.command, args: entry.args }) |
| 65 | + } |
| 66 | + |
| 67 | + const invocation = flattened[flattened.length - 1]! |
| 68 | + const stdoutText = |
| 69 | + invocation.command === "docker" && |
| 70 | + invocation.args.includes("inspect") && |
| 71 | + invocation.args.includes("bridge") |
| 72 | + ? "0.0.0.0\n" |
| 73 | + : "" |
| 74 | + const stdout = stdoutText.length === 0 ? Stream.empty : Stream.succeed(encode(stdoutText)) |
| 75 | + |
| 76 | + const process: CommandExecutor.Process = { |
| 77 | + [CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId, |
| 78 | + pid: CommandExecutor.ProcessId(1), |
| 79 | + exitCode: Effect.succeed(CommandExecutor.ExitCode(0)), |
| 80 | + isRunning: Effect.succeed(false), |
| 81 | + kill: (_signal) => Effect.void, |
| 82 | + stderr: Stream.empty, |
| 83 | + stdin: Sink.drain, |
| 84 | + stdout, |
| 85 | + toJSON: () => ({ |
| 86 | + _tag: "DockerUpTestProcess", |
| 87 | + command: invocation.command, |
| 88 | + args: invocation.args |
| 89 | + }), |
| 90 | + [Inspectable.NodeInspectSymbol]: () => ({ |
| 91 | + _tag: "DockerUpTestProcess", |
| 92 | + command: invocation.command, |
| 93 | + args: invocation.args |
| 94 | + }), |
| 95 | + toString: () => `[DockerUpTestProcess ${invocation.command}]` |
| 96 | + } |
| 97 | + |
| 98 | + return process |
| 99 | + }) |
| 100 | + |
| 101 | + return CommandExecutor.makeExecutor(start) |
| 102 | +} |
| 103 | + |
| 104 | +describe("runDockerUpIfNeeded with force", () => { |
| 105 | + it.effect("wipes compose orphans, removes container, then recreates", () => |
| 106 | + Effect.gen(function*(_) { |
| 107 | + const commands: Array<RecordedCommand> = [] |
| 108 | + const resolvedOutDir = "/tmp/docker-git-force-up" |
| 109 | + const config: CreateCommand["config"] = { |
| 110 | + containerName: "dg-force-test", |
| 111 | + serviceName: "dg-force-test", |
| 112 | + sshUser: "dev", |
| 113 | + sshPort: 2237, |
| 114 | + repoUrl: "https://github.com/org/repo.git", |
| 115 | + repoRef: "main", |
| 116 | + targetDir: "/home/dev/workspaces/org/repo", |
| 117 | + volumeName: "dg-force-test-home", |
| 118 | + dockerGitPath: `${resolvedOutDir}/.docker-git`, |
| 119 | + authorizedKeysPath: "/tmp/authorized_keys", |
| 120 | + envGlobalPath: `${resolvedOutDir}/.orch/env/global.env`, |
| 121 | + envProjectPath: `${resolvedOutDir}/docker-git.env`, |
| 122 | + codexAuthPath: `${resolvedOutDir}/.orch/auth/codex`, |
| 123 | + codexSharedAuthPath: `${resolvedOutDir}/.orch/auth/codex-shared`, |
| 124 | + codexHome: "/home/dev/.codex", |
| 125 | + geminiAuthPath: `${resolvedOutDir}/.orch/auth/gemini`, |
| 126 | + geminiHome: "/home/dev/.gemini", |
| 127 | + dockerNetworkMode: "project", |
| 128 | + dockerSharedNetworkName: "docker-git-shared", |
| 129 | + enableMcpPlaywright: true, |
| 130 | + pnpmVersion: "10.27.0", |
| 131 | + agentMode: undefined, |
| 132 | + agentAuto: false, |
| 133 | + clonedOnHostname: undefined, |
| 134 | + forkRepoUrl: undefined, |
| 135 | + gitTokenLabel: undefined, |
| 136 | + codexAuthLabel: undefined, |
| 137 | + claudeAuthLabel: undefined |
| 138 | + } |
| 139 | + |
| 140 | + const recordedExecutor = makeFakeExecutor(commands) |
| 141 | + const result = yield* _( |
| 142 | + runDockerUpIfNeeded(resolvedOutDir, config, { |
| 143 | + runUp: true, |
| 144 | + waitForClone: false, |
| 145 | + waitForAgent: false, |
| 146 | + force: true, |
| 147 | + forceEnv: false |
| 148 | + }).pipe( |
| 149 | + Effect.provideService(CommandExecutor.CommandExecutor, recordedExecutor), |
| 150 | + Effect.provideService(FileSystem.FileSystem, fakeFileSystem), |
| 151 | + Effect.provideService(Path.Path, fakePath) |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + expect(result).toBeUndefined() |
| 156 | + |
| 157 | + const downIndex = commands.findIndex(isDownWithRemoveOrphans) |
| 158 | + const rmMainIndex = commands.findIndex(isRmContainer("dg-force-test")) |
| 159 | + const rmBrowserIndex = commands.findIndex(isRmContainer("dg-force-test-browser")) |
| 160 | + const upIndex = commands.findIndex(isUp) |
| 161 | + |
| 162 | + expect(downIndex).toBeGreaterThanOrEqual(0) |
| 163 | + expect(rmMainIndex).toBeGreaterThan(downIndex) |
| 164 | + expect(rmBrowserIndex).toBeGreaterThan(rmMainIndex) |
| 165 | + expect(upIndex).toBeGreaterThan(rmBrowserIndex) |
| 166 | + }) |
| 167 | + ) |
| 168 | +}) |
0 commit comments