Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
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=273&format=json
176 linesbox-grants 100%Last change box-grants in version 273, 3 versions changed it

You're reading version 273. The newest is 318, and 3 versions since then changed this file. Go to the newestCompare with 318

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