Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

tailscaleServe.test.ts

177 lines6.1 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";
6import { HttpClient, HttpClientError, HttpClientResponse } from "effect/unstable/http";
7import { ChildProcessSpawner } from "effect/unstable/process";
8​
9import { acquireTailscaleServe, releaseTailscaleServe } from "./tailscaleServe.ts";
10​
11const encoder = new TextEncoder();
12​
13const descriptorJson = JSON.stringify({
14 environmentId: "environment-primary",
15 label: "primary",
16 platform: { os: "darwin", arch: "arm64" },
17 serverVersion: "0.0.38",
18 capabilities: {},
19});
20​
21const serveStatusJson = (localPort: number) =>
22 JSON.stringify({
23 TCP: { "10010": { HTTPS: true } },
24 Web: {
25 "host.tail.ts.net:10010": {
26 Handlers: { "/": { Proxy: `http://127.0.0.1:${String(localPort)}` } },
27 },
28 },
29 });
30​
31function mockHandle(stdout: string) {
32 return ChildProcessSpawner.makeHandle({
33 pid: ChildProcessSpawner.ProcessId(1),
34 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
35 isRunning: Effect.succeed(false),
36 kill: () => Effect.void,
37 unref: Effect.succeed(Effect.void),
38 stdin: Sink.drain,
39 stdout: Stream.make(encoder.encode(stdout)),
40 stderr: Stream.empty,
41 all: Stream.empty,
42 getInputFd: () => Sink.drain,
43 getOutputFd: () => Stream.empty,
44 });
46​
47/** Records every tailscale invocation and answers `serve status` with `status`. */
48function spawnerLayer(status: string) {
49 const commands: Array<ReadonlyArray<string>> = [];
50 const layer = Layer.succeed(
51 ChildProcessSpawner.ChildProcessSpawner,
52 ChildProcessSpawner.make((command) => {
53 const args = (command as unknown as { readonly args: ReadonlyArray<string> }).args;
54 commands.push(args);
55 return Effect.succeed(mockHandle(args[1] === "status" ? status : ""));
56 }),
57 );
58 return { commands, layer };
60​
61/** Answers the local descriptor probe: a live T3 server, or nothing listening. */
62function httpClientLayer(alive: boolean) {
63 const probed: Array<string> = [];
64 const layer = Layer.succeed(
65 HttpClient.HttpClient,
66 HttpClient.make((request) => {
67 probed.push(request.url);
68 return alive
69 ? Effect.succeed(
70 HttpClientResponse.fromWeb(
71 request,
72 new Response(descriptorJson, {
73 status: 200,
74 headers: { "content-type": "application/json" },
75 }),
76 ),
77 )
78 : Effect.fail(
79 new HttpClientError.HttpClientError({
80 reason: new HttpClientError.TransportError({
81 request,
82 description: "connection refused",
83 cause: new Error("ECONNREFUSED"),
84 }),
85 }),
86 );
87 }),
88 );
89 return { probed, layer };
91​
92const serveWrites = (commands: ReadonlyArray<ReadonlyArray<string>>) =>
93 commands.filter((args) => args[1] === "--bg");
94​
95describe("tailscaleServe", () => {
96 it.effect("writes the mapping when the serve port is free", () => {
97 const spawner = spawnerLayer("{}");
98 const http = httpClientLayer(false);
99​
100 return Effect.gen(function* () {
101 const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });
102​
103 assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
104 assert.deepEqual(serveWrites(spawner.commands), [
105 ["serve", "--bg", "--https=10010", "http://127.0.0.1:3773"],
106 ]);
107 assert.deepEqual(http.probed, []);
108 }).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
109 });
110​
111 it.effect("keeps a mapping that already fronts a live server on another port", () => {
112 const spawner = spawnerLayer(serveStatusJson(3773));
113 const http = httpClientLayer(true);
114​
115 return Effect.gen(function* () {
116 // The second desktop instance: same environment, next free backend port.
117 const owned = yield* acquireTailscaleServe({ localPort: 3774, servePort: 10010 });
118​
119 assert.equal(owned, null);
120 assert.deepEqual(serveWrites(spawner.commands), []);
121 assert.deepEqual(http.probed, ["http://127.0.0.1:3773/.well-known/t3/environment"]);
122 }).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
123 });
124​
125 it.effect("reclaims a mapping whose target no longer answers", () => {
126 const spawner = spawnerLayer(serveStatusJson(3774));
127 const http = httpClientLayer(false);
128​
129 return Effect.gen(function* () {
130 const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });
131​
132 assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
133 assert.deepEqual(serveWrites(spawner.commands), [
134 ["serve", "--bg", "--https=10010", "http://127.0.0.1:3773"],
135 ]);
136 }).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
137 });
138​
139 it.effect("adopts a mapping that already points at this server", () => {
140 const spawner = spawnerLayer(serveStatusJson(3773));
141 const http = httpClientLayer(true);
142​
143 return Effect.gen(function* () {
144 const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });
145​
146 assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
147 assert.deepEqual(serveWrites(spawner.commands), []);
148 assert.deepEqual(http.probed, []);
149 }).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
150 });
151​
152 it.effect("removes the mapping on shutdown while it still points here", () => {
153 const spawner = spawnerLayer(serveStatusJson(3773));
154​
155 return Effect.gen(function* () {
156 yield* releaseTailscaleServe({ localPort: 3773, servePort: 10010 });
157​
158 assert.deepEqual(
159 spawner.commands.filter((args) => args.includes("off")),
160 [["serve", "--https=10010", "off"]],
161 );
162 }).pipe(Effect.provide(spawner.layer));
163 });
164​
165 it.effect("leaves a mapping another server has taken over", () => {
166 const spawner = spawnerLayer(serveStatusJson(3775));
167​
168 return Effect.gen(function* () {
169 yield* releaseTailscaleServe({ localPort: 3773, servePort: 10010 });
170​
171 assert.deepEqual(
172 spawner.commands.filter((args) => args.includes("off")),
173 [],
174 );
175 }).pipe(Effect.provide(spawner.layer));
176 });
177});