Files
t3code/scripts/lib/dev-share.test.ts
T
Theo BrowneandClaude Opus 5 827469be4d Address CodeRabbit review
- The skill told agents both to never put a pairing URL in a response and
  to hand the shared one to the user. State the rule and its single
  exception explicitly: give the URL to the person who asked for access,
  never put it anywhere durable.
- A failed `tailscale serve` left the port unshared, because the stale-
  mapping clear had already run. The clear is still required (old
  mappings carry path routes that serving "/" would not replace), so say
  so in the error instead of letting an operator assume the previous
  mapping survived.
- Worktree-derived ports were described as collision-free. They are a
  preferred offset, not a guarantee — hashes can collide and occupied
  ports shift both values. Qualified in AGENTS.md, the skill, and
  docs/reference/scripts.md.

CodeRabbit also asked for tests around the pairing-url command; those
landed in 0555604de, which it had not yet seen.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-07-24 17:38:23 -07:00

72 lines
2.7 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 } from "./dev-share.ts";
const TAILNET_STATUS = JSON.stringify({ Self: { DNSName: "host.example.ts.net." } });
/**
* Answers `tailscale status --json` with a valid tailnet name, and lets each
* test decide how the `serve` call behaves.
*/
const spawnerLayer = (serve: { readonly exitCode: number; readonly stderr?: string }) =>
Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];
const isStatus = args.includes("status");
return Effect.succeed(
ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(isStatus ? 0 : serve.exitCode)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: isStatus ? Stream.make(new TextEncoder().encode(TAILNET_STATUS)) : Stream.empty,
stderr:
!isStatus && serve.stderr
? Stream.make(new TextEncoder().encode(serve.stderr))
: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
}),
);
}),
);
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({ exitCode: 0 })),
);
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({ 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");
}),
);
});