The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeSqlite from "node:sqlite"; |
| 2 | |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { assert, describe, it } from "@effect/vitest"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Fiber from "effect/Fiber"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as TestClock from "effect/testing/TestClock"; |
| 12 | |
| 13 | import * as DesktopConfig from "../app/DesktopConfig.ts"; |
| 14 | import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 15 | import * as DesktopHomeOwner from "./DesktopHomeOwner.ts"; |
| 16 | |
| 17 | const withTempHome = <A, E>( |
| 18 | run: ( |
| 19 | userdata: string, |
| 20 | ) => Effect.Effect< |
| 21 | A, |
| 22 | E, |
| 23 | DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem | Path.Path |
| 24 | >, |
| 25 | ) => |
| 26 | Effect.gen(function* () { |
| 27 | const fs = yield* FileSystem.FileSystem; |
| 28 | const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-home-owner-" }); |
| 29 | yield* fs.makeDirectory(`${home}/userdata`, { recursive: true }); |
| 30 | return yield* run(`${home}/userdata`).pipe( |
| 31 | Effect.provide( |
| 32 | DesktopEnvironment.layer({ |
| 33 | dirname: "/repo/apps/desktop/src", |
| 34 | homeDirectory: home, |
| 35 | platform: "linux", |
| 36 | processArch: "x64", |
| 37 | appVersion: "0.0.0", |
| 38 | appPath: "/repo", |
| 39 | isPackaged: true, |
| 40 | resourcesPath: "/missing/resources", |
| 41 | runningUnderArm64Translation: false, |
| 42 | }).pipe( |
| 43 | Layer.provide( |
| 44 | Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: home })), |
| 45 | ), |
| 46 | ), |
| 47 | ), |
| 48 | ); |
| 49 | }).pipe(Effect.provide(NodeServices.layer)); |
| 50 | |
| 51 | const readOwnerWithRuntimeFile = (contents: string | undefined) => |
| 52 | withTempHome((userdata) => |
| 53 | Effect.gen(function* () { |
| 54 | if (contents !== undefined) { |
| 55 | yield* (yield* FileSystem.FileSystem).writeFileString( |
| 56 | `${userdata}/server-runtime.json`, |
| 57 | contents, |
| 58 | ); |
| 59 | } |
| 60 | return yield* DesktopHomeOwner.readLiveHomeOwner(); |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | describe("DesktopHomeOwner.readLiveHomeOwner", () => { |
| 65 | it.effect("reports a live server recorded in server-runtime.json", () => |
| 66 | Effect.gen(function* () { |
| 67 | const owner = yield* readOwnerWithRuntimeFile( |
| 68 | JSON.stringify({ |
| 69 | version: 1, |
| 70 | pid: process.pid, |
| 71 | port: 3790, |
| 72 | origin: "http://127.0.0.1:3790", |
| 73 | startedAt: "2026-01-01T00:00:00.000Z", |
| 74 | serverVersion: "1.2.3", |
| 75 | }), |
| 76 | ); |
| 77 | assert.deepStrictEqual( |
| 78 | owner, |
| 79 | Option.some({ pid: process.pid, port: 3790, serverVersion: "1.2.3" }), |
| 80 | ); |
| 81 | assert.include( |
| 82 | DesktopHomeOwner.homeOwnedMessage(owner), |
| 83 | `PID ${process.pid}, port 3790, version 1.2.3`, |
| 84 | ); |
| 85 | }), |
| 86 | ); |
| 87 | |
| 88 | it.effect("ignores a missing, malformed or dead owner record", () => |
| 89 | Effect.gen(function* () { |
| 90 | assert.isTrue(Option.isNone(yield* readOwnerWithRuntimeFile(undefined))); |
| 91 | assert.isTrue(Option.isNone(yield* readOwnerWithRuntimeFile("{not json"))); |
| 92 | assert.isTrue( |
| 93 | Option.isNone(yield* readOwnerWithRuntimeFile(JSON.stringify({ pid: 0, port: 3790 }))), |
| 94 | ); |
| 95 | }), |
| 96 | ); |
| 97 | }); |
| 98 | |
| 99 | describe("DesktopHomeOwner.awaitHomeOwnershipRelease", () => { |
| 100 | it.effect("waits for a shutting-down owner to release the lock, then gives up", () => |
| 101 | withTempHome((userdata) => |
| 102 | Effect.gen(function* () { |
| 103 | assert.isTrue(yield* DesktopHomeOwner.awaitHomeOwnershipRelease()); |
| 104 | |
| 105 | const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`); |
| 106 | lock.exec("BEGIN EXCLUSIVE;"); |
| 107 | const released = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease()); |
| 108 | yield* TestClock.adjust("5 seconds"); |
| 109 | lock.close(); |
| 110 | yield* TestClock.adjust("1 second"); |
| 111 | assert.isTrue(yield* Fiber.join(released)); |
| 112 | |
| 113 | const held = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`); |
| 114 | held.exec("BEGIN EXCLUSIVE;"); |
| 115 | const owned = yield* Effect.forkChild(DesktopHomeOwner.awaitHomeOwnershipRelease()); |
| 116 | yield* TestClock.adjust("1 minute"); |
| 117 | assert.isFalse(yield* Fiber.join(owned)); |
| 118 | held.close(); |
| 119 | }), |
| 120 | ), |
| 121 | ); |
| 122 | }); |