Files
t3code/scripts/lib/dev-share.test.ts
T
Theo BrowneandClaude Opus 5 02594421d2 Simplify dev tooling after review
Four parallel cleanup reviews over the branch; the fixes that survived
verification:

- scripts/lib/dev-share.ts was a 216-line reimplementation of
  @t3tools/tailscale (the client the server's own --tailscale-serve
  uses). Now a thin wrapper adding only the dev-share semantics: the
  stale-mapping pre-clear and the cleared/not-cleared result. Picks up
  Windows tailscale.exe handling and command timeouts for free. The
  package's exit error gains a bounded stderrPreview so callers can
  recognize "handler does not exist" without a parallel spawner.
- The .git-is-a-file worktree check existed three times across two
  packages; now once in @t3tools/shared/devHome. dev-runner drops its
  duplicated T3CODE_HOME fallback so base-dir precedence lives in one
  function.
- The dev proxied-prefix list existed in apps/web's vite config and
  apps/server's http catch-all with nothing keeping them in sync; both
  now consume @t3tools/shared/devProxy. The vite proxy map is built
  from the list.
- PROJECTOR_NAMES and the projection-table delete order were pasted in
  both seeding scripts; now shared via scripts/lib/projection-tables.
- PairingGrantStore keyed the dev startup TTL by string-comparing the
  caller's subject; issueOneTimeToken takes an explicit
  purpose: "startup" instead, and the subject constant returns to
  EnvironmentAuth where its other uses live.
- dev-seed: iterate rows instead of materializing (unbounded copies
  held the whole table in memory and the spread had a ~128k-element
  ceiling); parameterized IN lists replace hand-rolled quoting.
- Smaller: cli/auth's layer stack deduped into environmentAuthLayer,
  Set-based dedupe, precomputed proxied prefixes, vite HOST parsed
  once, dead T3CODE_DEV_SHARE_URL removed, redundant tests dropped.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-25 21:45:24 -07:00

135 lines
5.0 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { ChildProcessSpawner } from "effect/unstable/process";
import { DevShareError, shareDevServer, unshareDevServer } from "./dev-share.ts";
const TAILNET_STATUS = JSON.stringify({ Self: { DNSName: "host.example.ts.net." } });
const NO_HANDLER_STDERR = "error: failed to remove web serve: handler does not exist";
interface CallResult {
readonly exitCode: number;
readonly stderr?: string;
}
const encode = (value: string) => Stream.make(new TextEncoder().encode(value));
/**
* Answers `tailscale status --json` with a valid tailnet name, and lets each
* test set the outcome of the `off` (pre-clear) and `serve` calls separately —
* they are the same subcommand and are told apart by the trailing `off`.
*/
const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallResult }) =>
Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];
const result: CallResult = args.includes("status")
? { exitCode: 0 }
: args.includes("off")
? (input.off ?? { exitCode: 0 })
: (input.serve ?? { exitCode: 0 });
return Effect.succeed(
ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: args.includes("status") ? encode(TAILNET_STATUS) : Stream.empty,
stderr: result.stderr ? encode(result.stderr) : Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
}),
);
}),
);
describe("unshareDevServer", () => {
it.effect("treats a removed mapping as cleared", () =>
Effect.gen(function* () {
const result = yield* unshareDevServer(5788).pipe(
Effect.provide(spawnerLayer({ off: { exitCode: 0 } })),
);
assert.isTrue(result.cleared);
}),
);
// `tailscale serve … off` exits 1 when the port had no mapping, which is the
// normal first-share case — the port is clear, so this must not be an error.
it.effect("treats a missing handler as cleared", () =>
Effect.gen(function* () {
const result = yield* unshareDevServer(5788).pipe(
Effect.provide(spawnerLayer({ off: { exitCode: 1, stderr: NO_HANDLER_STDERR } })),
);
assert.isTrue(result.cleared);
}),
);
it.effect("reports a genuine removal failure as not cleared", () =>
Effect.gen(function* () {
const result = yield* unshareDevServer(5788).pipe(
Effect.provide(spawnerLayer({ off: { exitCode: 1, stderr: "permission denied" } })),
);
assert.isFalse(result.cleared);
assert.include(result.detail, "permission denied");
}),
);
});
describe("shareDevServer", () => {
it.effect("returns the tailnet URL for the same port", () =>
Effect.gen(function* () {
const shared = yield* shareDevServer({ webPort: 5788 }).pipe(
Effect.provide(spawnerLayer({ off: { exitCode: 1, stderr: NO_HANDLER_STDERR } })),
);
assert.equal(shared.host, "host.example.ts.net");
assert.equal(shared.url, "https://host.example.ts.net:5788/");
}),
);
// The stale-mapping clear runs before serve, so a failure here leaves the
// port serving nothing. Saying only "serve failed" would let an operator
// assume their previous mapping survived.
it.effect("reports that the prior mapping was cleared when serve fails", () =>
Effect.gen(function* () {
const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe(
Effect.provide(
spawnerLayer({
off: { exitCode: 0 },
serve: { exitCode: 1, stderr: "port already in use" },
}),
),
Effect.flip,
);
assert.equal(error.reason, "serve-failed");
assert.include(error.message, "port already in use");
assert.include(error.message, "no longer served");
assert.include(error.message, "5788");
}),
);
// Serving over routes we could not remove yields a URL that loads but whose
// /ws and /api quietly point at a dead backend.
it.effect("refuses to serve when the existing mapping could not be cleared", () =>
Effect.gen(function* () {
const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe(
Effect.provide(spawnerLayer({ off: { exitCode: 1, stderr: "permission denied" } })),
Effect.flip,
);
assert.equal(error.reason, "serve-failed");
assert.include(error.message, "could not clear the existing mapping");
assert.include(error.message, "permission denied");
}),
);
});