diff --git a/apps/server/src/cli/auth.test.ts b/apps/server/src/cli/auth.test.ts new file mode 100644 index 0000000000..7466a6d527 --- /dev/null +++ b/apps/server/src/cli/auth.test.ts @@ -0,0 +1,138 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, describe, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; + +import { persistServerRuntimeState } from "../serverRuntimeState.ts"; +import { findLiveServerRuntimeState } from "./auth.ts"; + +/** A pid that cannot belong to a live process (above the usual pid_max). */ +const DEAD_PID = 0x7ffffffe; + +const writeRuntimeState = Effect.fn(function* (input: { + readonly baseDir: string; + readonly stateDir: "dev" | "userdata"; + readonly pid: number; + readonly port: number; + readonly devUrl?: string; +}) { + const path = yield* Path.Path; + yield* persistServerRuntimeState({ + path: path.join(input.baseDir, input.stateDir, "server-runtime.json"), + state: { + version: 1, + pid: input.pid, + port: input.port, + origin: `http://127.0.0.1:${String(input.port)}`, + ...(input.devUrl ? { devUrl: input.devUrl } : {}), + startedAt: "2026-07-20T00:00:00.000Z", + }, + }); +}); + +const makeBaseDir = Effect.fn(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-auth-cli-test-" }); + return { + baseDir, + // What the flag heuristic resolves to without --dev-url: the userdata dir. + serverRuntimeStatePath: path.join(baseDir, "userdata", "server-runtime.json"), + }; +}); + +describe("findLiveServerRuntimeState", () => { + it.effect("prefers the dev server when both state directories are live", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const config = yield* makeBaseDir(); + // The configured path points at userdata, but this command is about dev. + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "userdata", + pid: process.pid, + port: 16_601, + }); + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "dev", + pid: process.pid, + port: 16_602, + devUrl: "http://localhost:8888/", + }); + + const found = yield* findLiveServerRuntimeState(config); + + const live = Option.getOrThrow(found); + assert.equal(live.stateDir, path.join(config.baseDir, "dev")); + assert.equal(live.state.devUrl, "http://localhost:8888/"); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("falls back to a live non-dev server when no dev server is running", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const config = yield* makeBaseDir(); + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "userdata", + pid: process.pid, + port: 16_601, + }); + + const found = yield* findLiveServerRuntimeState(config); + + const live = Option.getOrThrow(found); + assert.equal(live.stateDir, path.join(config.baseDir, "userdata")); + assert.equal(live.state.port, 16_601); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + // A server killed with SIGKILL never clears its state file. + it.effect("ignores state files whose process is gone", () => + Effect.gen(function* () { + const config = yield* makeBaseDir(); + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "dev", + pid: DEAD_PID, + port: 16_602, + devUrl: "http://localhost:8888/", + }); + + assert.isTrue(Option.isNone(yield* findLiveServerRuntimeState(config))); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("skips a stale dev server in favor of a live one elsewhere", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const config = yield* makeBaseDir(); + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "dev", + pid: DEAD_PID, + port: 16_602, + devUrl: "http://localhost:8888/", + }); + yield* writeRuntimeState({ + baseDir: config.baseDir, + stateDir: "userdata", + pid: process.pid, + port: 16_601, + }); + + const live = Option.getOrThrow(yield* findLiveServerRuntimeState(config)); + assert.equal(live.stateDir, path.join(config.baseDir, "userdata")); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("reports nothing when no state files exist", () => + Effect.gen(function* () { + const config = yield* makeBaseDir(); + assert.isTrue(Option.isNone(yield* findLiveServerRuntimeState(config))); + }).pipe(Effect.provide(NodeServices.layer)), + ); +}); diff --git a/apps/server/src/cli/auth.ts b/apps/server/src/cli/auth.ts index 85450df9af..1096ce61e4 100644 --- a/apps/server/src/cli/auth.ts +++ b/apps/server/src/cli/auth.ts @@ -139,18 +139,23 @@ const pairingCreateCommand = Command.make("create", { * tokens into a database the running server never reads when guessed wrong. * So check both, and let the live server decide which one is right. */ -const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")(function* ( - config: ServerConfig.ServerConfig["Service"], +export const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")(function* ( + config: Pick, ) { const path = yield* Path.Path; - const candidates = [ + const candidatePaths = [ config.serverRuntimeStatePath, ...(["dev", "userdata"] as const).map((stateDir) => path.join(config.baseDir, stateDir, "server-runtime.json"), ), ].filter((candidate, index, all) => all.indexOf(candidate) === index); - for (const statePath of candidates) { + const live: Array<{ + readonly stateDir: string; + readonly state: PersistedServerRuntimeState; + }> = []; + + for (const statePath of candidatePaths) { const state = yield* readPersistedServerRuntimeState(statePath); if (Option.isNone(state)) { continue; @@ -159,11 +164,18 @@ const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")( // nothing is listening on. Minting against it produces a token the live // server rejects with `invalid_credential`. if (yield* isPersistedServerRuntimeStateLive(state.value)) { - return Option.some({ stateDir: path.dirname(statePath), state: state.value }); + live.push({ stateDir: path.dirname(statePath), state: state.value }); } } - return Option.none<{ readonly stateDir: string; readonly state: PersistedServerRuntimeState }>(); + // `devUrl` is only recorded by a server fronted by a web dev server, so it + // distinguishes the dev instance from a production-style one when both are + // running under the same base directory. This command is about dev, so a dev + // server wins regardless of which state directory the flags happened to + // resolve to. + return Option.fromNullishOr( + live.find((candidate) => candidate.state.devUrl !== undefined) ?? live[0], + ); }); /**