- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/news-stream.test.ts?version=285&format=json
182 linesbox-grants 100%Last change box-grants in version 285, 4 versions changed it
You're reading version 285. The newest is 318, and 2 versions since then changed this file.
1// abso news --stream and --follow (abso 690): the Monitor call, and holding the stream through closes and refusals.
3import assert from "node:assert/strict";
4import { test } from "node:test";
5import {
6 expiresAtMs,
7 type FollowDeps,
8 follow,
9 isRefusal,
10 MONITOR_TIMEOUT_MS,
11 monitorArgs,
12 NEWS_PROTOCOL,
13 type NewsTicket,
14 nextWait,
15 PING,
16 PING_MS,
17 PONG,
18 type Socket,
19 STREAM_MARK,
20 streamMarkUntil,
21 streamText,
22} from "../src/news-stream.ts";
24const NOW = 1_800_000_000_000;
25const ticket = (n: number): NewsTicket => ({
26 url: "wss://api.abso.dev/p/abso/a/fe/news/stream",
27 protocols: [NEWS_PROTOCOL, `abso-ticket.t${n}`],
28 expiresAt: NOW + 60_000,
29});
31test("--stream prints the Monitor call: the URL and protocols as the server gave them, 30 minutes, named for the seat", () => {
32 const args = monitorArgs(ticket(1), "abso", "fe");
33 assert.deepEqual(args, {
34 ws: { url: "wss://api.abso.dev/p/abso/a/fe/news/stream", protocols: ["abso-news.1", "abso-ticket.t1"] },
35 description: "abso news for fe in abso",
36 timeout_ms: MONITOR_TIMEOUT_MS,
37 });
38 assert.equal(MONITOR_TIMEOUT_MS, 30 * 60_000);
39 // The ticket never goes in the URL.
40 assert.doesNotMatch(args.ws.url, /t1/);
41 const text = streamText(ticket(1), "abso", "fe", NOW + 15_000).split("\n");
42 assert.match(text[0]!, /within 45 seconds/);
43 assert.match(text[0]!, /run abso news --stream again/);
44 assert.deepEqual(JSON.parse(text[1]!), args);
45 // The server sends the expiry as an ISO time (staging 1418); the countdown and the stream mark read it the same.
46 const iso = { ...ticket(1), expiresAt: new Date(NOW + 60_000).toISOString() };
47 assert.match(streamText(iso, "abso", "fe", NOW + 15_000), /within 45 seconds/);
48 assert.equal(streamMarkUntil(iso, NOW), NOW + 60_000 + MONITOR_TIMEOUT_MS);
49 assert.equal(expiresAtMs({ ...ticket(1), expiresAt: "soon" }, NOW), NOW);
50});
52test("a refusal close stops --follow; any other close opens again, waiting 1 s doubling to a minute", () => {
53 // 1008: a revoked key, or a fifth socket for the agent. A bad ticket is refused before the upgrade, so it closes 1006
54 // and is tried again with a new ticket; a refused key then fails the mint.
55 assert.equal(isRefusal(1008), true);
56 assert.equal(isRefusal(1006), false);
57 assert.equal(isRefusal(1001), false);
58 assert.equal(nextWait(1_000, 0), 2_000);
59 assert.equal(nextWait(32_000, 5_000), 60_000);
60 assert.equal(nextWait(60_000, 5_000), 60_000);
61 // A socket that lived a minute starts the wait over.
62 assert.equal(nextWait(60_000, 60_000), 1_000);
63});
65/** A world for follow(): each connect gets the next script of frames and a close. */
66function world(scripts: { frames: string[]; close: { code: number; reason?: string }; livedMs?: number }[]) {
67 let now = NOW;
68 let n = 0;
69 const lines: string[] = [];
70 const notes: string[] = [];
71 const sent: string[] = [];
72 const slept: number[] = [];
73 const opened: { url: string; protocols: readonly string[] }[] = [];
74 const pings: (() => void)[] = [];
75 let stop = false;
76 const deps: FollowDeps = {
77 mint: async () => ticket(++n),
78 connect: (url, protocols) => {
79 opened.push({ url, protocols });
80 const script = scripts[opened.length - 1];
81 const socket: Socket = {
82 onopen: null,
83 onmessage: null,
84 onclose: null,
85 onerror: null,
86 send: (text) => sent.push(text),
87 close: () => {},
88 };
89 queueMicrotask(() => {
90 socket.onopen?.();
91 if (script === undefined) {
92 stop = true;
93 socket.onclose?.({ code: 1000, reason: "" });
94 return;
95 }
96 for (const pinged of pings.splice(0)) pinged();
97 for (const data of script.frames) socket.onmessage?.({ data });
98 now += script.livedMs ?? 1_000;
99 socket.onclose?.({ code: script.close.code, reason: script.close.reason ?? "" });
100 });
101 return socket;
102 },
103 write: (line) => lines.push(line),
104 note: (line) => notes.push(line),
105 sleep: async (ms) => {
106 slept.push(ms);
107 },
108 now: () => now,
109 every: (ms, fn) => {
110 assert.equal(ms, PING_MS);
111 pings.push(fn);
112 return () => {};
113 },
114 stopped: () => stop,
115 };
116 return { deps, lines, notes, sent, slept, opened };
117}
119test("--follow prints each item once, skips the server's pongs, and opens a new socket with a new ticket after a close", async () => {
120 const w = world([
121 { frames: ["fe told you: hello", PONG, "an ask from cloud: ship it?"], close: { code: 1006 } },
122 { frames: ["second socket's item"], close: { code: 1001 }, livedMs: 120_000 },
123 ]);
124 const ended = await follow(w.deps);
125 assert.deepEqual(ended, { ended: "stopped", reason: "stopped" });
126 assert.deepEqual(w.lines, ["fe told you: hello", "an ask from cloud: ship it?", "second socket's item"]);
127 // Each socket used its own ticket; each ticket was used once.
128 assert.deepEqual(
129 w.opened.map((o) => o.protocols[1]),
130 ["abso-ticket.t1", "abso-ticket.t2", "abso-ticket.t3"],
131 );
132 // The first socket lived a second: wait 2 s. The second lived two minutes: start over at 1 s.
133 assert.deepEqual(w.slept, [2_000, 1_000]);
134 assert.equal(w.notes.length, 0);
135});
137test("--follow keeps an idle socket open with a ping the object answers without waking", async () => {
138 const w = world([{ frames: [], close: { code: 1006 } }]);
139 const held: number[] = [];
140 w.deps.held = (until) => held.push(until);
141 await follow(w.deps);
142 assert.deepEqual(w.sent, [PING]);
143 // The stream mark is renewed on open and at each ping, two pings ahead, so the mod's poll stands down meanwhile.
144 // The first socket opens and pings; the second opens a second later.
145 assert.deepEqual(held, [NOW + 2 * PING_MS, NOW + 2 * PING_MS, NOW + 1_000 + 2 * PING_MS]);
146});
148test("--stream's mark holds for the Monitor's 30 minutes after the ticket's minute", () => {
149 assert.equal(streamMarkUntil(ticket(1), NOW), NOW + 60_000 + MONITOR_TIMEOUT_MS);
150 assert.equal(STREAM_MARK, "news-stream.json");
151});
153test("a refused ticket or a refusal close ends --follow; a ticket that can't be had is tried again", async () => {
154 const refusedClose = world([{ frames: ["one item"], close: { code: 1008, reason: "key revoked" } }]);
155 assert.deepEqual(await follow(refusedClose.deps), { ended: "refused", reason: "key revoked" });
156 assert.deepEqual(refusedClose.lines, ["one item"]);
158 const w = world([{ frames: [], close: { code: 1000 } }]);
159 let tries = 0;
160 const mint = w.deps.mint;
161 w.deps.mint = async () => {
162 tries++;
163 if (tries === 1) throw Object.assign(new Error("could not reach the server"), { status: undefined });
164 if (tries === 3)
165 throw Object.assign(new Error("POST /a/fe/news/ticket answered 401: no such key"), { status: 401 });
166 return mint();
167 };
168 const ended = await follow(w.deps);
169 assert.equal(ended.ended, "refused");
170 assert.match(ended.reason, /401/);
171 assert.equal(w.notes.length, 1);
172 assert.match(w.notes[0]!, /no ticket: could not reach the server; trying again in 1 s/);
173 assert.deepEqual(w.lines, []);
175 // A server without the stream (before abso 690) answers the ticket 404: stop, don't retry.
176 const old = world([]);
177 old.deps.mint = async () => {
178 throw Object.assign(new Error("POST /a/fe/news/ticket answered 404: not found"), { status: 404 });
179 };
180 assert.equal((await follow(old.deps)).ended, "refused");
181 assert.deepEqual(old.slept, []);
182});