fix(web): reset stream filters and exclude warning rows from counts

Reset the stream panel's filter and request state when the exchange changes.
Keep recording-limit warnings visible while reporting only actual messages
in the header, including first-page and warning-only counts.

Add message_total to the stream page without changing raw pagination totals
or existing Go paging signatures. Cover the count and pagination boundaries
in the stream and HTTP route tests.
This commit is contained in:
APZN
2026-11-23 10:26:56 +09:00
parent 51fe31a653
commit 7e555e86d7
10 changed files with 225 additions and 50 deletions
+80 -11
View File
@@ -42,6 +42,7 @@ function loadModule(file) {
const { createTrafficStreamPager } = loadModule(path.join(web, "src/lib/traffic-stream.ts"));
const { api } = loadModule(path.join(web, "src/lib/api.ts"));
const { trafficStreamMessages } = loadModule(path.join(web, "src/lib/mock/data.ts"));
const plain = (value) => JSON.parse(JSON.stringify(value));
// Await an event-loop turn so promises crossing the vm context also settle.
const flush = () => setImmediate();
@@ -68,6 +69,7 @@ function pendingPager() {
test("API pages the selected direction and keeps original seqs and the limit warning", async () => {
const all = await api.trafficStream("x-ws");
assert.equal(all.total, 121);
assert.equal(all.message_total, 120);
assert.equal(all.items.length, 50);
assert.deepEqual(plain(all.items.slice(0, 2).map((item) => item.direction)), ["c2s", "s2c"]);
@@ -76,6 +78,8 @@ test("API pages the selected direction and keeps original seqs and the limit war
const last = await api.trafficStream("x-ws", 50, 50, direction);
assert.equal(first.total, 61);
assert.equal(last.total, 61);
assert.equal(first.message_total, 60);
assert.equal(last.message_total, 60);
assert.equal(first.items.length, 50);
assert.equal(last.items.length, 11);
assert.equal(first.items[0].seq, direction === "c2s" ? 1 : 2);
@@ -92,40 +96,66 @@ test("mock stream details expose both protocols and an empty SSE client directio
assert.equal((await api.trafficExchange("x-ws")).stream_kind, "websocket");
assert.equal((await api.trafficExchange("x-sse")).stream_kind, "sse");
assert.equal((await api.trafficExchange("x-9")).stream_kind, undefined);
assert.deepEqual(plain(await api.trafficStream("x-sse", 0, 50, "c2s")), { items: [], total: 0 });
assert.equal((await api.trafficStream("x-sse", 0, 50, "s2c")).total, 3);
assert.deepEqual(plain(await api.trafficStream("x-sse", 0, 50, "c2s")), {
items: [],
total: 0,
message_total: 0,
});
const events = await api.trafficStream("x-sse", 0, 50, "s2c");
assert.equal(events.total, 3);
assert.equal(events.message_total, 3);
});
test("warning-only results keep the recording warning with a zero message total", async () => {
const id = "test-sse-with-limit";
const warning = { ...message(4, "meta"), type: "meta" };
trafficStreamMessages[id] = [...trafficStreamMessages["x-sse"], warning];
try {
const page = await api.trafficStream(id, 0, 50, "c2s");
assert.equal(page.total, 1);
assert.equal(page.message_total, 0);
assert.deepEqual(plain(page.items), [warning]);
const beyond = await api.trafficStream(id, 1, 50, "c2s");
assert.equal(beyond.total, 1);
assert.equal(beyond.message_total, 0);
assert.deepEqual(plain(beyond.items), []);
} finally {
delete trafficStreamMessages[id];
}
});
test("changing the filter or exchange resets pagination and discards a stale response", async () => {
const p = pendingPager();
p.pager.select("ws", "c2s");
p.calls[0].resolve({ items: [message(1), message(3)], total: 60 });
p.calls[0].resolve({ items: [message(1), message(3)], total: 60, message_total: 60 });
await flush();
p.pager.loadMore();
p.pager.select("ws", "s2c");
assert.deepEqual(plain(p.state().items), []);
assert.equal(p.state().total, 0);
assert.equal(p.state().messageTotal, 0);
assert.equal(p.state().loadingMore, false);
assert.deepEqual(p.calls[2].args, ["ws", 0, 50, "s2c"]);
p.calls[1].resolve({ items: [message(5)], total: 60 });
p.calls[1].resolve({ items: [message(5)], total: 60, message_total: 60 });
await flush();
assert.deepEqual(plain(p.state().items), []);
assert.equal(p.state().loading, true);
p.pager.select("sse", "s2c");
p.calls[2].resolve({ items: [message(2, "s2c")], total: 60 });
p.calls[3].resolve({ items: [message(1, "s2c")], total: 1 });
p.calls[2].resolve({ items: [message(2, "s2c")], total: 60, message_total: 60 });
p.calls[3].resolve({ items: [message(1, "s2c")], total: 1, message_total: 1 });
await flush();
assert.deepEqual(p.calls[3].args, ["sse", 0, 50, "s2c"]);
assert.deepEqual(plain(p.state().items.map((item) => item.seq)), [1]);
assert.equal(p.state().total, 1);
assert.equal(p.state().messageTotal, 1);
});
test("Load more uses the filtered offset, and Refresh invalidates a pending page and its error", async () => {
const p = pendingPager();
p.pager.select("ws", "s2c");
p.calls[0].resolve({ items: [message(2, "s2c"), message(4, "s2c")], total: 4 });
p.calls[0].resolve({ items: [message(2, "s2c"), message(4, "s2c")], total: 4, message_total: 3 });
await flush();
p.pager.loadMore();
p.pager.loadMore();
@@ -138,13 +168,16 @@ test("Load more uses the filtered offset, and Refresh invalidates a pending page
await flush();
assert.equal(p.state().error, null);
assert.equal(p.state().loading, true);
p.calls[2].resolve({ items: [message(2, "s2c")], total: 2 });
p.calls[2].resolve({ items: [message(2, "s2c")], total: 2, message_total: 1 });
await flush();
assert.equal(p.state().messageTotal, 1);
// All actual messages are loaded, but the warning must still be fetched.
p.pager.loadMore();
assert.deepEqual(p.calls[3].args, ["ws", 1, 50, "s2c"]);
p.calls[3].resolve({ items: [{ ...message(121, "meta"), type: "meta" }], total: 2 });
p.calls[3].resolve({ items: [{ ...message(121, "meta"), type: "meta" }], total: 2, message_total: 1 });
await flush();
assert.deepEqual(plain(p.state().items.map((item) => item.seq)), [2, 121]);
assert.equal(p.state().messageTotal, 1);
p.pager.loadMore();
assert.equal(p.calls.length, 4);
});
@@ -153,7 +186,7 @@ test("cancellation suppresses late results, and a current error can be refreshed
const p = pendingPager();
p.pager.select("ws", "");
p.pager.cancel();
p.calls[0].resolve({ items: [message(1)], total: 1 });
p.calls[0].resolve({ items: [message(1)], total: 1, message_total: 1 });
await flush();
assert.deepEqual(plain(p.state().items), []);
p.pager.select("sse", "c2s");
@@ -163,9 +196,45 @@ test("cancellation suppresses late results, and a current error can be refreshed
assert.equal(p.state().loading, false);
p.pager.refresh();
assert.deepEqual(p.calls[2].args, ["sse", 0, 50, "c2s"]);
p.calls[2].resolve({ items: [], total: 0 });
p.calls[2].resolve({ items: [], total: 0, message_total: 0 });
await flush();
assert.equal(p.state().error, null);
assert.equal(p.state().loading, false);
assert.equal(p.state().total, 0);
assert.equal(p.state().messageTotal, 0);
});
test("the pager keeps separate message and row totals through pagination and exchange changes", async () => {
let state;
const calls = [];
const pager = createTrafficStreamPager(
(...args) => {
calls.push(args);
return api.trafficStream(...args);
},
(next) => {
state = next;
},
);
pager.select("x-ws", "c2s");
await flush();
assert.equal(state.items.length, 50);
assert.equal(state.total, 61);
assert.equal(state.messageTotal, 60);
assert.ok(state.items.every((item) => item.type !== "meta"));
pager.loadMore();
await flush();
assert.deepEqual(calls[1], ["x-ws", 50, 50, "c2s"]);
assert.equal(state.items.length, 61);
assert.equal(state.items.filter((item) => item.type !== "meta").length, 60);
assert.equal(state.messageTotal, 60);
assert.equal(state.items.at(-1).type, "meta");
pager.loadMore();
assert.equal(calls.length, 2);
pager.select("x-sse", "");
assert.equal(state.messageTotal, 0);
await flush();
assert.equal(state.total, 3);
assert.equal(state.messageTotal, 3);
});