|
| 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 FileSystem from "effect/FileSystem"; |
| 7 | +import * as Layer from "effect/Layer"; |
| 8 | +import * as Option from "effect/Option"; |
| 9 | +import * as Path from "effect/Path"; |
| 10 | + |
| 11 | +import * as DesktopConfig from "../app/DesktopConfig.ts"; |
| 12 | +import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 13 | +import * as DesktopHomeOwner from "./DesktopHomeOwner.ts"; |
| 14 | + |
| 15 | +const withTempHome = <A, E>( |
| 16 | + run: ( |
| 17 | + userdata: string, |
| 18 | + ) => Effect.Effect< |
| 19 | + A, |
| 20 | + E, |
| 21 | + DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem | Path.Path |
| 22 | + >, |
| 23 | +) => |
| 24 | + Effect.gen(function* () { |
| 25 | + const fs = yield* FileSystem.FileSystem; |
| 26 | + const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-home-owner-" }); |
| 27 | + yield* fs.makeDirectory(`${home}/userdata`, { recursive: true }); |
| 28 | + return yield* run(`${home}/userdata`).pipe( |
| 29 | + Effect.provide( |
| 30 | + DesktopEnvironment.layer({ |
| 31 | + dirname: "/repo/apps/desktop/src", |
| 32 | + homeDirectory: home, |
| 33 | + platform: "linux", |
| 34 | + processArch: "x64", |
| 35 | + appVersion: "0.0.0", |
| 36 | + appPath: "/repo", |
| 37 | + isPackaged: true, |
| 38 | + resourcesPath: "/missing/resources", |
| 39 | + runningUnderArm64Translation: false, |
| 40 | + }).pipe( |
| 41 | + Layer.provide( |
| 42 | + Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({ T3CODE_HOME: home })), |
| 43 | + ), |
| 44 | + ), |
| 45 | + ), |
| 46 | + ); |
| 47 | + }).pipe(Effect.provide(NodeServices.layer)); |
| 48 | + |
| 49 | +const readOwnerWithRuntimeFile = (contents: string | undefined) => |
| 50 | + withTempHome((userdata) => |
| 51 | + Effect.gen(function* () { |
| 52 | + if (contents !== undefined) { |
| 53 | + yield* (yield* FileSystem.FileSystem).writeFileString( |
| 54 | + `${userdata}/server-runtime.json`, |
| 55 | + contents, |
| 56 | + ); |
| 57 | + } |
| 58 | + return yield* DesktopHomeOwner.readLiveHomeOwner(); |
| 59 | + }), |
| 60 | + ); |
| 61 | + |
| 62 | +describe("DesktopHomeOwner.readLiveHomeOwner", () => { |
| 63 | + it.effect("reports a live server recorded in server-runtime.json", () => |
| 64 | + Effect.gen(function* () { |
| 65 | + const owner = yield* readOwnerWithRuntimeFile( |
| 66 | + JSON.stringify({ |
| 67 | + version: 1, |
| 68 | + pid: process.pid, |
| 69 | + port: 3790, |
| 70 | + origin: "http://127.0.0.1:3790", |
| 71 | + startedAt: "2026-01-01T00:00:00.000Z", |
| 72 | + serverVersion: "1.2.3", |
| 73 | + }), |
| 74 | + ); |
| 75 | + assert.deepStrictEqual( |
| 76 | + owner, |
| 77 | + Option.some({ pid: process.pid, port: 3790, serverVersion: "1.2.3" }), |
| 78 | + ); |
| 79 | + assert.include( |
| 80 | + DesktopHomeOwner.homeOwnedMessage(owner), |
| 81 | + `PID ${process.pid}, port 3790, version 1.2.3`, |
| 82 | + ); |
| 83 | + }), |
| 84 | + ); |
| 85 | + |
| 86 | + it.effect("ignores a missing, malformed or dead owner record", () => |
| 87 | + Effect.gen(function* () { |
| 88 | + assert.isTrue(Option.isNone(yield* readOwnerWithRuntimeFile(undefined))); |
| 89 | + assert.isTrue(Option.isNone(yield* readOwnerWithRuntimeFile("{not json"))); |
| 90 | + assert.isTrue( |
| 91 | + Option.isNone(yield* readOwnerWithRuntimeFile(JSON.stringify({ pid: 0, port: 3790 }))), |
| 92 | + ); |
| 93 | + }), |
| 94 | + ); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("DesktopHomeOwner.isHomeOwnershipLocked", () => { |
| 98 | + it.effect("reports the ownership lock only while a server holds it", () => |
| 99 | + withTempHome((userdata) => |
| 100 | + Effect.gen(function* () { |
| 101 | + assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked()); |
| 102 | + const lock = new NodeSqlite.DatabaseSync(`${userdata}/server-owner.sqlite`); |
| 103 | + lock.exec("BEGIN EXCLUSIVE;"); |
| 104 | + assert.isTrue(yield* DesktopHomeOwner.isHomeOwnershipLocked()); |
| 105 | + lock.close(); |
| 106 | + assert.isFalse(yield* DesktopHomeOwner.isHomeOwnershipLocked()); |
| 107 | + }), |
| 108 | + ), |
| 109 | + ); |
| 110 | +}); |
0 commit comments