Mouth: room tail on chat_pending.v3 (20 lines a room, 60 in all, unacked past her cursor, speakable rooms only)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
be44a09d52
commit
8e9ffb32f4
8 changed files with 212 additions and 17 deletions
Binary file not shown.
BIN
tests/fixtures/persona-chat-v5/gamecult.persona.chat_pending.v3.tail.msgpack
vendored
Normal file
BIN
tests/fixtures/persona-chat-v5/gamecult.persona.chat_pending.v3.tail.msgpack
vendored
Normal file
Binary file not shown.
Binary file not shown.
|
|
@ -20,6 +20,7 @@ const runtime = loadRuntime(cultLibRoot);
|
|||
const { msgpack } = runtime;
|
||||
const fixtureDir = resolve(import.meta.dirname, "fixtures", "persona-chat-v1");
|
||||
|
||||
const tailLine = { roomAlias: "#operator:gamecult.org", eventId: "$t1", atUnixMs: 1_700_000_000_000, sender: "@meta:gamecult.org", form: "message", basis: null, text: "earlier" };
|
||||
const delivery = {
|
||||
seq: 7, roomAlias: "#operator:gamecult.org", roomId: "!abc:gamecult.org", eventId: "$e1", sender: "@meta:gamecult.org", senderDisplay: "Meta",
|
||||
text: "hello eureka", atUnixMs: 1761700000000,
|
||||
|
|
@ -93,6 +94,7 @@ test("the brake reader reads a real CultCache control store", async () => {
|
|||
test("two clients served at once each get their own answer on their own session", async () => {
|
||||
const mouth = {
|
||||
pendingAfter: () => [],
|
||||
tail: async () => [],
|
||||
ack: async () => {},
|
||||
// Slow enough that the two requests are in flight together.
|
||||
send: async request => { await new Promise(r => setTimeout(r, 150)); return { requestId: request.requestId, eventId: `$${request.requestId}`, refusal: null }; },
|
||||
|
|
@ -138,6 +140,7 @@ test("the RUDP endpoint serves pending, ack and send over CultNet, and answers a
|
|||
const calls = { acks: [], sends: [], reads: [] };
|
||||
const mouth = {
|
||||
pendingAfter: after => (after < 7 ? [delivery] : []),
|
||||
tail: async () => [tailLine],
|
||||
ack: async through => { calls.acks.push(through); },
|
||||
read: async request => { calls.reads.push(request); return null; },
|
||||
send: async request => { calls.sends.push(request); return request.text === "muted" ? { requestId: request.requestId, eventId: null, refusal: "Brake" } : { requestId: request.requestId, eventId: "$sent", refusal: null }; },
|
||||
|
|
@ -177,7 +180,9 @@ test("the RUDP endpoint serves pending, ack and send over CultNet, and answers a
|
|||
put(docs.kinds.pendingRequestV3, "eureka", docs.pendingRequestTuple({ persona: "eureka", afterSeq: 0 }));
|
||||
document = await reply;
|
||||
assert.equal(document.schemaId, docs.kinds.pendingV3, "the kind asked by names the answer");
|
||||
assert.deepEqual(docs.parsePendingV3(msgpack.decode(document.payload)).deliveries[0], { ...delivery, basis: null, form: "message" });
|
||||
const v3 = docs.parsePendingV3(msgpack.decode(document.payload));
|
||||
assert.deepEqual(v3.deliveries[0], { ...delivery, basis: null, form: "message" });
|
||||
assert.deepEqual(v3.tail, [tailLine], "the v3 answer carries the room tail");
|
||||
|
||||
put(docs.kinds.ack, "eureka:7", docs.ackTuple({ persona: "eureka", throughSeq: 7 }));
|
||||
reply = nextReply();
|
||||
|
|
@ -322,6 +327,10 @@ const v5Dir = resolve(root, "tests", "fixtures", "persona-chat-v5");
|
|||
const v5Samples = {
|
||||
"gamecult.persona.chat_pending_request.v2": { tuple: docs.pendingRequestTuple({ persona: "eureka", afterSeq: 3 }), parse: docs.parsePendingRequest },
|
||||
"gamecult.persona.chat_pending.v3.tag": { tuple: docs.pendingV3Tuple({ persona: "eureka", deliveries: [{ ...delivery, isDirect: false, basis: "tag", form: "message" }] }), parse: docs.parsePendingV3 },
|
||||
"gamecult.persona.chat_pending.v3.tail": { tuple: docs.pendingV3Tuple({ persona: "eureka", deliveries: [], tail: [
|
||||
{ roomAlias: "#operator:gamecult.org", eventId: "$e0", atUnixMs: 1_700_000_000_000, sender: "@meta:gamecult.org", form: "message", basis: null, text: "earlier" },
|
||||
{ roomAlias: "#operator:gamecult.org", eventId: "$e1", atUnixMs: 1_700_000_001_000, sender: "@meta:gamecult.org", form: "notice", basis: "tag", text: "hello eureka" },
|
||||
] }), parse: docs.parsePendingV3 },
|
||||
"gamecult.persona.chat_pending.v3.unproven-notice": { tuple: docs.pendingV3Tuple({ persona: "eureka", deliveries: [{ ...delivery, isDirect: false, mentions: [], basis: null, form: "notice" }] }), parse: docs.parsePendingV3 },
|
||||
};
|
||||
for (const [name, { tuple, parse }] of Object.entries(v5Samples)) {
|
||||
|
|
@ -331,7 +340,9 @@ for (const [name, { tuple, parse }] of Object.entries(v5Samples)) {
|
|||
if (process.env.UPDATE_FIXTURES === "1") { await mkdir(v5Dir, { recursive: true }); await writeFile(file, bytes); }
|
||||
assert.deepEqual(bytes, await readFile(file), "wire bytes changed: Huginn decodes these fixtures");
|
||||
assert.deepEqual(parse(msgpack.decode(bytes)), parse(tuple));
|
||||
if (name.includes("pending.v3")) assert.equal(msgpack.decode(bytes)[1][0].length, 14);
|
||||
if (name.includes("pending.v3")) assert.equal(msgpack.decode(bytes).length, 3, "persona, deliveries, tail");
|
||||
if (name.endsWith("v3.tag") || name.endsWith("unproven-notice")) assert.equal(msgpack.decode(bytes)[1][0].length, 14);
|
||||
if (name.endsWith("v3.tail")) assert.equal(msgpack.decode(bytes)[2][0].length, 7);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -370,7 +381,7 @@ test("RoomMoved is v2 only, and the v2 documents are strict about arity and newe
|
|||
|
||||
test("the RUDP endpoint answers a v1 send with the three-field receipt and a v2 send with the four-field one", async () => {
|
||||
const seen = [];
|
||||
const mouth = { pendingAfter: () => [], ack: async () => {}, send: async request => {
|
||||
const mouth = { pendingAfter: () => [], tail: async () => [], ack: async () => {}, send: async request => {
|
||||
seen.push(request);
|
||||
return request.basis ? { requestId: request.requestId, eventId: null, refusal: "RoomMoved", newer: ["$a"] } : { requestId: request.requestId, eventId: "$sent", refusal: null, newer: [] };
|
||||
} };
|
||||
|
|
|
|||
|
|
@ -1475,3 +1475,103 @@ test("addressing facts: a target that failed is not refetched within a minute, a
|
|||
assert.equal(fetched(), 2, "past the minute: read again");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
// The room tail (docs/bridge.md, "Room tail"): her rooms' recent talk past her ack cursor, bounded, as data on the v3 answer.
|
||||
const OPERATOR = "!operator:gamecult.test";
|
||||
const lines = (room, n, from = 0, sender = META) => Array.from({ length: n }, (_, i) => message(`$${room.slice(1, 4)}${from + i}`, sender, `line ${from + i}`, 10 + from + i));
|
||||
async function tailed({ preload, direct, member, events = [], act = async () => {} } = {}) {
|
||||
const hs = await fakeHomeserver({ preload, direct, member, sync: since => since === null ? { next_batch: "b0" } : { next_batch: "b1", ...joined(events.length ? { [ROOM]: { timeline: { events } } } : {}) } });
|
||||
try {
|
||||
const mouth = mouthFor(hs);
|
||||
await mouth.syncOnce();
|
||||
await act(mouth);
|
||||
return { tail: await mouth.tail(), mouth, hs };
|
||||
} finally { await hs.close(); }
|
||||
}
|
||||
|
||||
test("room tail: 600 lines in a room keep the newest 20, oldest first", async () => {
|
||||
const { tail } = await tailed({ preload: { [ROOM]: lines(ROOM, 600) } });
|
||||
assert.equal(tail.length, 20);
|
||||
assert.deepEqual(tail.map(l => l.text), Array.from({ length: 20 }, (_, i) => `line ${580 + i}`));
|
||||
assert.deepEqual(Object.keys(tail[0]).sort(), ["atUnixMs", "basis", "eventId", "form", "roomAlias", "sender", "text"]);
|
||||
assert.equal(tail[0].roomAlias, `#swarm:${HS_NAME}`);
|
||||
assert.equal(tail[0].sender, META);
|
||||
assert.equal(tail[0].form, "message");
|
||||
assert.equal(tail[0].basis, null, "unproven, not 'not for her'");
|
||||
});
|
||||
|
||||
test("room tail: a quiet room, with nothing delivered, still returns its recent lines", async () => {
|
||||
const { tail } = await tailed({ preload: { [OPERATOR]: lines(OPERATOR, 3) } });
|
||||
assert.deepEqual(tail.map(l => l.text), ["line 0", "line 1", "line 2"]);
|
||||
assert.ok(tail.every(l => l.roomAlias === `#operator:${HS_NAME}`));
|
||||
});
|
||||
|
||||
test("room tail: a room she is not in the speaking set of is absent, and a DM she holds is present with basis direct", async () => {
|
||||
const STRANGER = "!stranger:gamecult.test", DM = "!mydm:gamecult.test";
|
||||
const { tail } = await tailed({
|
||||
preload: { [STRANGER]: lines(STRANGER, 2), [DM]: lines(DM, 2), [ROOM]: lines(ROOM, 1) },
|
||||
member: [...Object.values(ROOMS), STRANGER, DM],
|
||||
direct: { [META]: [DM] },
|
||||
});
|
||||
assert.deepEqual([...new Set(tail.map(l => l.roomAlias))].sort(), [DM, `#swarm:${HS_NAME}`]);
|
||||
assert.ok(tail.filter(l => l.roomAlias === DM).every(l => l.basis === "direct"));
|
||||
assert.ok(!tail.some(l => l.eventId.startsWith("$str")));
|
||||
});
|
||||
|
||||
test("room tail: about 60 lines over all rooms, the newest, whatever the room count", async () => {
|
||||
const dms = ["!d1:gamecult.test", "!d2:gamecult.test", "!d3:gamecult.test", "!d4:gamecult.test"];
|
||||
const preload = { [ROOM]: lines(ROOM, 30, 0), [OPERATOR]: lines(OPERATOR, 30, 30) };
|
||||
dms.forEach((dm, i) => { preload[dm] = lines(dm, 30, 60 + 30 * i); });
|
||||
const { tail } = await tailed({ preload, direct: { [META]: dms }, member: [...Object.values(ROOMS), ...dms] });
|
||||
assert.equal(tail.length, 60);
|
||||
assert.deepEqual(tail.map(l => l.atUnixMs), tail.map(l => l.atUnixMs).sort((a, b) => a - b), "oldest first");
|
||||
assert.equal(Math.min(...tail.map(l => l.atUnixMs)), 110, "the oldest kept is the 60th newest of the 120 offered");
|
||||
});
|
||||
|
||||
test("room tail: lines at or before her ack cursor are gone, later ones stay, and mention basis rides on the line", async () => {
|
||||
const events = [
|
||||
withContent("$a", META, { body: "first" }, 3000),
|
||||
withContent("$b", META, { body: "tag", "m.mentions": { user_ids: [SELF] } }, 3001),
|
||||
];
|
||||
const hs = await fakeHomeserver({ sync: since => since === null ? { next_batch: "b0" } : { next_batch: "b1", ...joined({ [ROOM]: { timeline: { events } } }) } });
|
||||
try {
|
||||
const mouth = mouthFor(hs);
|
||||
await mouth.syncOnce();
|
||||
const made = await mouth.syncOnce();
|
||||
assert.equal(made.length, 2);
|
||||
assert.deepEqual((await mouth.tail()).map(l => [l.eventId, l.basis]), [["$a", null], ["$b", "tag"]]);
|
||||
await mouth.ack(made[0].seq);
|
||||
assert.deepEqual((await mouth.tail()).map(l => l.eventId), ["$b"], "unacked past the cursor only");
|
||||
await mouth.ack(made[1].seq);
|
||||
assert.deepEqual(await mouth.tail(), []);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("room tail: a body is clipped like a context line, and an unreadable room is absent without silencing the others", async () => {
|
||||
const { tail } = await tailed({ preload: { [ROOM]: [message("$big", META, "x".repeat(5000), 10)] } });
|
||||
assert.equal(Buffer.byteLength(tail[0].text), docs.limits.contextBytes);
|
||||
const hs = await fakeHomeserver({ failContext: true, sync: since => ({ next_batch: since ?? "b0" }) });
|
||||
try {
|
||||
const mouth = mouthFor(hs);
|
||||
await mouth.syncOnce();
|
||||
assert.deepEqual(await mouth.tail(), []);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("room tail: only the v3 answer carries it; the v2 answer a v1 request gets is unchanged", async () => {
|
||||
const { tail } = await tailed({ preload: { [ROOM]: lines(ROOM, 2) } });
|
||||
const pack = { encode: v => Buffer.from(JSON.stringify(v)) };
|
||||
assert.deepEqual(docs.parsePendingV3(pageOf("eureka", [], pack, () => {}, true, tail)).tail.map(l => l.text), ["line 0", "line 1"]);
|
||||
const v2 = pageOf("eureka", [], pack, () => {}, false, tail);
|
||||
assert.equal(v2.length, 2);
|
||||
assert.deepEqual(docs.parsePending(v2), { persona: "eureka", deliveries: [] });
|
||||
assert.throws(() => docs.parsePendingV3(["eureka", []]), /3-field/);
|
||||
});
|
||||
|
||||
test("room tail: the reply byte budget trims the oldest tail lines, never a delivery", async () => {
|
||||
const pack = { encode: v => Buffer.from(JSON.stringify(v)) };
|
||||
const big = Array.from({ length: 60 }, (_, i) => ({ roomAlias: "#r:x", eventId: `$${i}`, atUnixMs: i, sender: "@a:x", form: "message", basis: null, text: "y".repeat(5000) }));
|
||||
const kept = docs.parsePendingV3(pageOf("eureka", [], pack, () => {}, true, big)).tail;
|
||||
assert.ok(kept.length > 0 && kept.length < 60);
|
||||
assert.equal(kept.at(-1).eventId, "$59", "the newest survive");
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue