|
| 1 | +import { NodeContext } from "@effect/platform-node" |
| 2 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 3 | +import * as Path from "@effect/platform/Path" |
| 4 | +import { describe, expect, it } from "@effect/vitest" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +import { |
| 8 | + controllerBuildSkillerEnvKey, |
| 9 | + controllerGpuModeEnvKey, |
| 10 | + ensureSkillerSubmoduleInitialized, |
| 11 | + prepareControllerRevision |
| 12 | +} from "../../src/docker-git/controller-compose.js" |
| 13 | +import { controllerRevisionEnvKey } from "../../src/docker-git/controller-revision.js" |
| 14 | +import { commandExecutorLayer } from "./fixtures/command-executor.js" |
| 15 | + |
| 16 | +const expectedSkillerSubmoduleCommand = |
| 17 | + "git submodule update --init --checkout third_party/skiller-desktop-skills-manager" |
| 18 | +const skillerPackageRelativePath = "third_party/skiller-desktop-skills-manager/package.json" |
| 19 | + |
| 20 | +const temporaryControllerRoot = Effect.gen(function*(_) { |
| 21 | + const fs = yield* _(FileSystem.FileSystem) |
| 22 | + return yield* _(fs.makeTempDirectoryScoped({ prefix: "docker-git-controller-compose-" })) |
| 23 | +}) |
| 24 | + |
| 25 | +const writeRootFile = ( |
| 26 | + rootDir: string, |
| 27 | + relativePath: string, |
| 28 | + contents: string |
| 29 | +) => |
| 30 | + Effect.all({ |
| 31 | + fs: FileSystem.FileSystem, |
| 32 | + path: Path.Path |
| 33 | + }).pipe( |
| 34 | + Effect.flatMap(({ fs, path }) => { |
| 35 | + const absolutePath = path.join(rootDir, relativePath) |
| 36 | + return fs.makeDirectory(path.dirname(absolutePath), { recursive: true }).pipe( |
| 37 | + Effect.zipRight(fs.writeFileString(absolutePath, contents)) |
| 38 | + ) |
| 39 | + }) |
| 40 | + ) |
| 41 | + |
| 42 | +const writeMinimalCompose = (rootDir: string) => |
| 43 | + writeRootFile(rootDir, "docker-compose.yml", "services:\n api:\n image: docker-git-api\n") |
| 44 | + |
| 45 | +const writeSkillerPackage = (rootDir: string) => |
| 46 | + writeRootFile(rootDir, skillerPackageRelativePath, "{\"name\":\"skiller-desktop-skills-manager\"}\n") |
| 47 | + |
| 48 | +const withWorkingDirectory = (nextCwd: string) => |
| 49 | + Effect.acquireRelease( |
| 50 | + Effect.sync(() => { |
| 51 | + const previousCwd = process.cwd() |
| 52 | + process.chdir(nextCwd) |
| 53 | + return previousCwd |
| 54 | + }), |
| 55 | + (previousCwd) => |
| 56 | + Effect.sync(() => { |
| 57 | + process.chdir(previousCwd) |
| 58 | + }) |
| 59 | + ) |
| 60 | + |
| 61 | +const setOptionalEnv = (key: string, value: string | undefined): void => { |
| 62 | + if (value === undefined) { |
| 63 | + Reflect.deleteProperty(process.env, key) |
| 64 | + return |
| 65 | + } |
| 66 | + process.env[key] = value |
| 67 | +} |
| 68 | + |
| 69 | +const withControllerEnv = (entries: ReadonlyArray<readonly [string, string | undefined]>) => |
| 70 | + Effect.acquireRelease( |
| 71 | + Effect.sync(() => { |
| 72 | + const previousEntries: Array<readonly [string, string | undefined]> = entries.map(([ |
| 73 | + key |
| 74 | + ]) => [key, process.env[key]]) |
| 75 | + for (const [key, value] of entries) { |
| 76 | + setOptionalEnv(key, value) |
| 77 | + } |
| 78 | + return previousEntries |
| 79 | + }), |
| 80 | + (previousEntries) => |
| 81 | + Effect.sync(() => { |
| 82 | + for (const [key, value] of previousEntries) { |
| 83 | + setOptionalEnv(key, value) |
| 84 | + } |
| 85 | + }) |
| 86 | + ) |
| 87 | + |
| 88 | +type PreparedRevision = { |
| 89 | + readonly persistedRevision: string | undefined |
| 90 | + readonly revision: string |
| 91 | +} |
| 92 | + |
| 93 | +type PrepareRevisionFixture = { |
| 94 | + readonly buildSkillerMode: string | undefined |
| 95 | + readonly includeSkillerPackage: boolean |
| 96 | +} |
| 97 | + |
| 98 | +const prepareRevisionInTemporaryRoot = ({ |
| 99 | + buildSkillerMode, |
| 100 | + includeSkillerPackage |
| 101 | +}: PrepareRevisionFixture) => |
| 102 | + Effect.scoped( |
| 103 | + Effect.gen(function*(_) { |
| 104 | + const rootDir = yield* _(temporaryControllerRoot) |
| 105 | + yield* _(writeMinimalCompose(rootDir)) |
| 106 | + if (includeSkillerPackage) { |
| 107 | + yield* _(writeSkillerPackage(rootDir)) |
| 108 | + } |
| 109 | + yield* _(withWorkingDirectory(rootDir)) |
| 110 | + yield* _( |
| 111 | + withControllerEnv([ |
| 112 | + [controllerBuildSkillerEnvKey, buildSkillerMode], |
| 113 | + [controllerGpuModeEnvKey, undefined], |
| 114 | + [controllerRevisionEnvKey, undefined] |
| 115 | + ]) |
| 116 | + ) |
| 117 | + |
| 118 | + const revision = yield* _(prepareControllerRevision()) |
| 119 | + return { persistedRevision: process.env[controllerRevisionEnvKey], revision } |
| 120 | + }) |
| 121 | + ).pipe(Effect.provide(NodeContext.layer)) |
| 122 | + |
| 123 | +const expectPreparedRevision = (prepared: PreparedRevision, pattern: RegExp): void => { |
| 124 | + expect(prepared.revision).toMatch(pattern) |
| 125 | + expect(prepared.persistedRevision).toBe(prepared.revision) |
| 126 | +} |
| 127 | + |
| 128 | +describe("controller compose preparation", () => { |
| 129 | + it.effect("does not initialize the Skiller submodule when package metadata already exists", () => |
| 130 | + Effect.scoped( |
| 131 | + Effect.gen(function*(_) { |
| 132 | + const rootDir = yield* _(temporaryControllerRoot) |
| 133 | + yield* _(writeSkillerPackage(rootDir)) |
| 134 | + |
| 135 | + yield* _(ensureSkillerSubmoduleInitialized(rootDir)) |
| 136 | + }) |
| 137 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 138 | + |
| 139 | + it.effect("reports a typed failure when submodule initialization cannot provide package metadata", () => |
| 140 | + Effect.scoped( |
| 141 | + Effect.gen(function*(_) { |
| 142 | + const rootDir = yield* _(temporaryControllerRoot) |
| 143 | + const startedCommands: Array<string> = [] |
| 144 | + |
| 145 | + const error = yield* _( |
| 146 | + ensureSkillerSubmoduleInitialized(rootDir).pipe( |
| 147 | + Effect.provide(commandExecutorLayer((command) => { |
| 148 | + startedCommands.push([command.command, ...command.args].join(" ")) |
| 149 | + return { exitCode: 128, stderr: "fatal: no submodule mapping found", stdout: "" } |
| 150 | + })), |
| 151 | + Effect.provide(NodeContext.layer), |
| 152 | + Effect.flip |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + expect(error._tag).toBe("ControllerBootstrapError") |
| 157 | + expect(error.message).toContain(expectedSkillerSubmoduleCommand) |
| 158 | + expect(startedCommands).toEqual([expectedSkillerSubmoduleCommand]) |
| 159 | + }) |
| 160 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 161 | + |
| 162 | + it.effect("prepares and persists host controller revisions for Skiller build modes", () => |
| 163 | + Effect.gen(function*(_) { |
| 164 | + const enabled = yield* _(prepareRevisionInTemporaryRoot({ |
| 165 | + buildSkillerMode: undefined, |
| 166 | + includeSkillerPackage: true |
| 167 | + })) |
| 168 | + const disabled = yield* _(prepareRevisionInTemporaryRoot({ |
| 169 | + buildSkillerMode: "0", |
| 170 | + includeSkillerPackage: false |
| 171 | + })) |
| 172 | + |
| 173 | + expectPreparedRevision(enabled, /^[a-f0-9]{16}-none-skiller1$/u) |
| 174 | + expectPreparedRevision(disabled, /^[a-f0-9]{16}-none-skiller0$/u) |
| 175 | + })) |
| 176 | +}) |
0 commit comments