-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate-project-state-sync-order.test.ts
More file actions
183 lines (160 loc) · 6.38 KB
/
create-project-state-sync-order.test.ts
File metadata and controls
183 lines (160 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as Command from "@effect/platform/Command"
import * as CommandExecutor from "@effect/platform/CommandExecutor"
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { NodeContext } from "@effect/platform-node"
import { describe, expect, it } from "@effect/vitest"
import { Effect } from "effect"
import * as Inspectable from "effect/Inspectable"
import * as Sink from "effect/Sink"
import * as Stream from "effect/Stream"
import { vi } from "vitest"
import type { CreateCommand, TemplateConfig } from "../../src/core/domain.js"
import { createProject } from "../../src/usecases/actions/create-project.js"
vi.mock("../../src/usecases/actions/ports.js", () => ({
resolveSshPort: (config: CreateCommand["config"]) => Effect.succeed(config)
}))
type RecordedCommand = {
readonly command: string
readonly args: ReadonlyArray<string>
}
const withTempDir = <A, E, R>(
use: (tempDir: string) => Effect.Effect<A, E, R>
): Effect.Effect<A, E, R | FileSystem.FileSystem> =>
Effect.scoped(
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const tempDir = yield* _(
fs.makeTempDirectoryScoped({
prefix: "docker-git-state-sync-order-"
})
)
return yield* _(use(tempDir))
})
)
const commandIncludes = (args: ReadonlyArray<string>, needle: string): boolean => args.includes(needle)
const decideExitCode = (cmd: RecordedCommand): number => {
if (cmd.command === "git" && cmd.args[0] === "rev-parse") {
// Auto-sync should detect "not a repo" and exit early.
return 1
}
if (cmd.command === "docker" && cmd.args[0] === "exec") {
if (commandIncludes(cmd.args, "/run/docker-git/clone.failed")) {
return 1
}
if (commandIncludes(cmd.args, "/run/docker-git/clone.done")) {
return 0
}
}
return 0
}
const decideStdout = (cmd: RecordedCommand): string => {
if (cmd.command === "docker" && cmd.args[0] === "inspect") {
return ""
}
return ""
}
const makeFakeExecutor = (recorded: Array<RecordedCommand>): CommandExecutor.CommandExecutor => {
const start = (command: Command.Command): Effect.Effect<CommandExecutor.Process, never> =>
Effect.gen(function*(_) {
const flattened = Command.flatten(command)
for (const entry of flattened) {
recorded.push({ command: entry.command, args: entry.args })
}
const last = flattened[flattened.length - 1]
const invocation: RecordedCommand = { command: last.command, args: last.args }
const exit = decideExitCode(invocation)
const stdoutText = decideStdout(invocation)
const stdout = stdoutText.length === 0 ? Stream.empty : Stream.succeed(new TextEncoder().encode(stdoutText))
const process: CommandExecutor.Process = {
[CommandExecutor.ProcessTypeId]: CommandExecutor.ProcessTypeId,
pid: CommandExecutor.ProcessId(1),
exitCode: Effect.succeed(CommandExecutor.ExitCode(exit)),
isRunning: Effect.succeed(false),
kill: (_signal) => Effect.void,
stderr: Stream.empty,
stdin: Sink.drain,
stdout,
toJSON: () => ({ _tag: "TestProcess", command: invocation.command, args: invocation.args, exit }),
[Inspectable.NodeInspectSymbol]: () => ({ _tag: "TestProcess", command: invocation.command, args: invocation.args }),
toString: () => `[TestProcess ${invocation.command}]`
}
return process
})
return CommandExecutor.makeExecutor(start)
}
const makeCommand = (root: string, outDir: string, path: Path.Path): CreateCommand => {
const template: TemplateConfig = {
containerName: "dg-test",
serviceName: "dg-test",
sshUser: "dev",
sshPort: 2222,
repoUrl: "https://github.com/org/repo.git",
repoRef: "main",
targetDir: "/home/dev/org/repo",
volumeName: "dg-test-home",
dockerGitPath: path.join(root, ".docker-git"),
authorizedKeysPath: path.join(root, "authorized_keys"),
envGlobalPath: path.join(root, ".orch/env/global.env"),
envProjectPath: path.join(root, ".orch/env/project.env"),
codexAuthPath: path.join(root, ".orch/auth/codex"),
codexSharedAuthPath: path.join(root, ".orch/auth/codex-shared"),
codexHome: "/home/dev/.codex",
dockerNetworkMode: "shared",
dockerSharedNetworkName: "docker-git-shared",
enableMcpPlaywright: false,
pnpmVersion: "10.27.0"
}
return {
_tag: "Create",
config: template,
outDir,
runUp: true,
openSsh: false,
force: true,
forceEnv: false,
waitForClone: true
}
}
// CHANGE: verify autoSyncState probe precedes docker compose up in recorded command sequence
// WHY: git reset --hard in autoSyncState deletes and recreates .orch/auth/codex; running it
// after docker up invalidates the bind-mount inode inside the container
// QUOTE(ТЗ): n/a
// REF: issue-158
// SOURCE: n/a
// FORMAT THEOREM: ∀p: stateSyncProbeIndex(p) < dockerComposeUpIndex(p)
// PURITY: SHELL
// EFFECT: Effect<void, never, NodeContext>
// INVARIANT: .orch/auth/codex inode is stable when docker compose up runs
// COMPLEXITY: O(n) where n = |recorded commands|
const isStateSyncProbe = (cmd: RecordedCommand): boolean =>
cmd.command === "git" && cmd.args[0] === "rev-parse"
const isDockerComposeUp = (cmd: RecordedCommand): boolean =>
cmd.command === "docker" &&
cmd.args.includes("compose") &&
cmd.args.includes("up")
describe("createProject (state sync order)", () => {
it.effect("autoSyncState probe runs before docker compose up", () =>
withTempDir((root) =>
Effect.gen(function*(_) {
const path = yield* _(Path.Path)
const outDir = path.join(root, "project")
const recorded: Array<RecordedCommand> = []
const executor = makeFakeExecutor(recorded)
const command = makeCommand(root, outDir, path)
yield* _(
createProject(command).pipe(
Effect.provideService(CommandExecutor.CommandExecutor, executor)
)
)
const stateSyncProbeIndex = recorded.findIndex(isStateSyncProbe)
const dockerComposeUpIndex = recorded.findIndex(isDockerComposeUp)
expect(stateSyncProbeIndex).toBeGreaterThanOrEqual(0)
expect(dockerComposeUpIndex).toBeGreaterThanOrEqual(0)
// INVARIANT: ∀p: stateSyncProbeIndex(p) < dockerComposeUpIndex(p)
expect(stateSyncProbeIndex).toBeLessThan(dockerComposeUpIndex)
})
)
.pipe(Effect.provide(NodeContext.layer))
)
})