Skip to content

Commit 566895f

Browse files
test(tlon): prove chunked outbound over real urbit loopback
1 parent 983ff81 commit 566895f

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Tlon outbound loopback tests exercise the real HTTP poke path against a mock
2+
// urbit ship: authenticate then one bounded poke per chunked text unit.
3+
import { once } from "node:events";
4+
import * as http from "node:http";
5+
import { afterEach, describe, expect, it } from "vitest";
6+
import { tlonPlugin } from "./channel.js";
7+
8+
const TEXT_LIMIT = 10_000;
9+
10+
type TlonPoke = {
11+
app?: string;
12+
mark?: string;
13+
json?: {
14+
ship?: string;
15+
diff?: {
16+
delta?: {
17+
add?: {
18+
memo?: { content?: unknown; author?: string };
19+
};
20+
};
21+
};
22+
};
23+
};
24+
25+
/** Join the plain-string run of a Tlon story the same way a reader reconstructs text. */
26+
function extractStoryText(content: unknown): string {
27+
const verses = Array.isArray(content) ? (content as unknown[]) : [];
28+
const parts: string[] = [];
29+
for (const verse of verses) {
30+
if (
31+
verse &&
32+
typeof verse === "object" &&
33+
"inline" in verse &&
34+
Array.isArray((verse as { inline?: unknown }).inline)
35+
) {
36+
for (const item of (verse as { inline: unknown[] }).inline) {
37+
if (typeof item === "string") {
38+
parts.push(item);
39+
}
40+
}
41+
}
42+
}
43+
return parts.join("");
44+
}
45+
46+
describe("tlon outbound chunking loopback", () => {
47+
let server: http.Server | undefined;
48+
49+
async function listenLoopback(handler: http.RequestListener): Promise<number> {
50+
server = http.createServer(handler);
51+
server.on("clientError", (_err, socket) => socket.destroy());
52+
server.listen(0, "127.0.0.1");
53+
await once(server, "listening");
54+
const address = server.address();
55+
if (!address || typeof address === "string") {
56+
throw new Error("expected loopback server address");
57+
}
58+
return address.port;
59+
}
60+
61+
afterEach(async () => {
62+
if (server) {
63+
await new Promise<void>((resolve, reject) => {
64+
server?.close((err) => (err ? reject(err) : resolve()));
65+
server?.closeAllConnections?.();
66+
});
67+
server = undefined;
68+
}
69+
});
70+
71+
it("delivers each chunked unit as a bounded independent poke the urbit transport accepts", async () => {
72+
const pokes: TlonPoke[] = [];
73+
const rejectedOversized: Array<{ length: number }> = [];
74+
let loginCount = 0;
75+
const port = await listenLoopback((req, res) => {
76+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
77+
if (req.method === "POST" && url.pathname === "/~/login") {
78+
loginCount += 1;
79+
res.writeHead(200, {
80+
"Content-Type": "text/plain",
81+
"set-cookie": "urbauth-~zod=mock-cookie",
82+
});
83+
res.end("ok");
84+
return;
85+
}
86+
if (req.method === "PUT" && url.pathname.startsWith("/~/channel/")) {
87+
let body = "";
88+
req.setEncoding("utf8");
89+
req.on("data", (chunk: string) => {
90+
body += chunk;
91+
});
92+
req.on("end", () => {
93+
const received = JSON.parse(body) as TlonPoke[];
94+
pokes.push(...received);
95+
const oversized = received.some(
96+
(poke) =>
97+
extractStoryText(poke.json?.diff?.delta?.add?.memo?.content).length > TEXT_LIMIT,
98+
);
99+
if (oversized) {
100+
rejectedOversized.push({ length: received.length });
101+
res.writeHead(413, { "Content-Type": "text/plain" });
102+
res.end("memo too long");
103+
return;
104+
}
105+
res.writeHead(200, { "Content-Type": "text/plain" });
106+
res.end("ok");
107+
});
108+
return;
109+
}
110+
res.writeHead(404, { "Content-Type": "text/plain" });
111+
res.end("not found");
112+
});
113+
114+
const cfg = {
115+
channels: {
116+
tlon: {
117+
enabled: true,
118+
ship: "~zod",
119+
url: `http://127.0.0.1:${port}`,
120+
code: "mock-code",
121+
network: { dangerouslyAllowPrivateNetwork: true },
122+
},
123+
},
124+
};
125+
126+
const outbound = tlonPlugin.outbound;
127+
if (!outbound) {
128+
throw new Error("expected tlon plugin to declare an outbound adapter");
129+
}
130+
const chunker = outbound.chunker;
131+
if (!chunker || outbound.textChunkLimit !== TEXT_LIMIT) {
132+
throw new Error("expected tlon outbound to declare a bounded chunker");
133+
}
134+
135+
const text = "x".repeat(10_001);
136+
const chunks = chunker(text, TEXT_LIMIT);
137+
for (const chunk of chunks) {
138+
await outbound.sendText?.({ cfg, to: "~nec", text: chunk });
139+
}
140+
141+
expect(chunks.length).toBeGreaterThan(1);
142+
expect(pokes).toHaveLength(chunks.length);
143+
expect(loginCount).toBeGreaterThan(0);
144+
// Every unit fits the transport limit, so the urbit transport accepts each poke.
145+
expect(rejectedOversized).toEqual([]);
146+
const delivered = pokes.map((poke) =>
147+
extractStoryText(poke.json?.diff?.delta?.add?.memo?.content),
148+
);
149+
expect(delivered.every((part) => part.length <= TEXT_LIMIT)).toBe(true);
150+
expect(delivered.join("")).toBe(text);
151+
});
152+
});

0 commit comments

Comments
 (0)