The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | |
| 7 | import { acquireServiceSelectionLock } from "./serviceSelectionLock.ts"; |
| 8 | |
| 9 | it.layer(NodeServices.layer)("service selection lock", (it) => { |
| 10 | it.effect("reclaims a lease from an earlier boot even when its PID is live again", () => |
| 11 | Effect.gen(function* () { |
| 12 | const fs = yield* FileSystem.FileSystem; |
| 13 | const path = yield* Path.Path; |
| 14 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-selection-lock-" }); |
| 15 | const selectionPath = path.join(root, "service-state.json"); |
| 16 | const lock = `${selectionPath}.selection-lock`; |
| 17 | const stale = `${process.pid}.1.00000000-0000-4000-8000-000000000000`; |
| 18 | yield* fs.makeDirectory(lock, { recursive: true }); |
| 19 | yield* fs.writeFileString(path.join(lock, stale), ""); |
| 20 | |
| 21 | const release = yield* Effect.promise(() => acquireServiceSelectionLock(selectionPath)); |
| 22 | const owners = yield* fs.readDirectory(lock); |
| 23 | assert.equal(owners.length, 1); |
| 24 | assert.notEqual(owners[0], stale); |
| 25 | assert.isTrue(owners[0]!.startsWith(`${process.pid}.`)); |
| 26 | release(); |
| 27 | assert.isFalse(yield* fs.exists(lock)); |
| 28 | }), |
| 29 | ); |
| 30 | }); |