Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { beginLoadClock, detectTrace, setDetectTrace, traceDetect, playbackTrace, setPlaybackTrace, tracePlayback, playbackTraceHandle, watchLoad,} from "./trace";
/** * The trace itself, tested because a watchdog nobody has seen bark is a watchdog that has * usually been quietly turned off. Two properties matter enough to be worth a file: that it * says the useful thing when it is on, and that it costs the *program* nothing when it is * off — the second is not a style preference, it is the reason the detail is passed as a * function, and it is the thing that broke the suite the first time this was wired in. */
const lines = (): string[] => (vi.mocked(console.debug).mock.calls as unknown as string[][]).map((a) => String(a[0]));
beforeEach(() => { vi.restoreAllMocks(); vi.spyOn(console, "debug").mockImplementation(() => undefined); vi.useFakeTimers();});
afterEach(() => { vi.useRealTimers(); setPlaybackTrace(null); // leave the default ("nobody has said") for whatever runs next});
it("is silent under the test runner, where a chatty probe is worse than none", () => { expect(playbackTrace()).toBe(false); tracePlayback("anything", "s1"); expect(vi.mocked(console.debug)).not.toHaveBeenCalled();});
it("says what happened, about which track, and how long it took", () => { setPlaybackTrace(true); const since = beginLoadClock(); tracePlayback("served 200", "al-1-s0", since, () => ({ container: "flac", try: "1/4" }));
const [line] = lines(); expect(line).toContain("[player]"); expect(line).toContain("al-1-s0"); // the id, so a trace of a queue reads as one track expect(line).toContain("served 200"); expect(line).toContain("container=flac"); expect(line).toContain("try=1/4"); expect(line).toMatch(/\+\d+(\.\d)?s/); // the elapsed marker, in seconds});
/** * The property the suite learned about the hard way: building a log line must not run any of * the code that line is about. A detail object handed over ready-made has already read * `res.headers` and `buffer.numberOfChannels` — and off, in a test or in production, that is * the trace reaching into a fixture that never promised those fields. */it("does not build the line at all when it is off", () => { setPlaybackTrace(false); const built = vi.fn(() => ({ dangerous: (undefined as unknown as { read: () => string }).read(), })); expect(() => tracePlayback("nothing to see", "s1", beginLoadClock(), built)).not.toThrow(); expect(built).not.toHaveBeenCalled();});
it("says what a load is waiting for, on a watch, until it is disarmed", () => { setPlaybackTrace(true); const since = beginLoadClock(); let waiting = "the response has not arrived"; const disarm = watchLoad( "s1", since, () => waiting, () => ({ bytes: 4096 }), );
// Nothing before the first stall: a fast load is not a story, and printing ordinary loads at // four seconds would make the one line that matters scroll away. vi.advanceTimersByTime(3999); expect(lines().some((l) => l.includes("still waiting"))).toBe(false);
vi.advanceTimersByTime(2); const stalled = lines().filter((l) => l.includes("still waiting")); expect(stalled).toHaveLength(1); expect(stalled[0]).toContain("the response has not arrived"); expect(stalled[0]).toContain("bytes=4096"); // read when the line was printed, not when it began
// The wait moved, and so does the next line — which is the difference between a slow // download and a decode that has eaten the page, and the only thing the first line cannot tell you. waiting = "the browser's decoder has the bytes and has not answered"; vi.advanceTimersByTime(8000); const later = lines().filter((l) => l.includes("still waiting")); expect(later).toHaveLength(2); expect(later[1]).toContain("decoder");
disarm(); vi.advanceTimersByTime(60000); expect(lines().filter((l) => l.includes("still waiting"))).toHaveLength(2);});
it("never leaves a watch running after the load settles, on any path", async () => { setPlaybackTrace(true); const since = beginLoadClock(); const disarm = watchLoad("s1", since, () => "the response"); disarm(); disarm(); // the timer is cleared once; a second call is a caller who is allowed to be casual vi.advanceTimersByTime(30000); expect(lines().some((l) => l.includes("still waiting"))).toBe(false);});
/** * The console handle, because the whole point of the gate is that it can be reached from a * phone over the tailnet while the track is still hanging. */it("answers to the console, and remembers being answered", () => { // the handle the dev build puts on the window, called the way a person would from a console expect(playbackTraceHandle()).toBe(false); expect(playbackTraceHandle(true)).toBe(true); expect(playbackTraceHandle()).toBe(true); // ...and it is the same setting the rest of the module reads, not a parallel one: the line // below only appears because the handle above is what turned the printer on. tracePlayback("after being switched on", "s1"); expect(lines().some((l) => l.includes("after being switched on"))).toBe(true); expect(playbackTraceHandle(false)).toBe(false); tracePlayback("after being switched off", "s1"); expect(lines().some((l) => l.includes("after being switched off"))).toBe(false);});
/** * The detector is a channel of its own, for the one situation that needs it: a `c` press * that came back *not sure*. Following the playback switch by default is what makes the * answer arrive without asking — nobody is going to think to switch on the numbers channel * before the symptom appears — and being switchable apart from it is what keeps those dozen * lines of numbers off an ordinary listening session. */it("gives the detector its own switch, and follows the player's by default", () => { expect(detectTrace()).toBe(false); // off, with nothing said, under the runner setPlaybackTrace(true); expect(detectTrace()).toBe(true); // ...and on when the playback trace is, unasked
setDetectTrace(false); expect(detectTrace()).toBe(false); expect(playbackTrace()).toBe(true); // the two are not the same setting traceDetect("invisible", "s1"); expect(lines().some((l) => l.includes("invisible"))).toBe(false);
setDetectTrace(true); traceDetect("the numbers", "s1", 1200, () => ({ best: 0.902, floor: 0.93, priced: 49611 })); const [line] = lines().filter((l) => l.includes("the numbers")); expect(line).toContain("[detect]"); // a distinct tag: this is not playback talking expect(line).toContain("s1"); expect(line).toContain("best=0.902"); // three decimals — the argument lives in that digit expect(line).toContain("floor=0.93"); expect(line).toContain("priced=49611"); expect(line).toContain("+1.2s");
setDetectTrace(null); // back to following, for whoever runs next setPlaybackTrace(null);});
/** * Off means nothing was read: the numbers come from a function the caller hands over, and a * stats object built on the way to a printer that is switched off is a probe reaching into * the measurement — which is how a trace ends up changing what it watches. */it("does not ask the detector for its numbers when nobody is listening", () => { setDetectTrace(false); const built = vi.fn(() => ({ best: (undefined as unknown as { n: number }).n })); expect(() => traceDetect("nothing", "s1", 0, built)).not.toThrow(); expect(built).not.toHaveBeenCalled();});