Initial import of streamline
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import { fetchEvidenceList, fetchReportSource } from "../src/lib/reportSource";
|
||||
|
||||
/**
|
||||
* 一次抽屉打开 = 每种资源一个请求:内联报告和「全屏」FixReportPanel(以及证据弹窗)
|
||||
* 共用 lib/reportSource 的模块级缓存。钉住三条:在途合并、成功结果复用、失败不缓存。
|
||||
*/
|
||||
describe("fetchReportSource", () => {
|
||||
test("同一条目并发 + 随后再取,只打一次网络", async () => {
|
||||
const orig = globalThis.fetch;
|
||||
let calls = 0;
|
||||
globalThis.fetch = (async () => { calls++; return new Response("# report", { status: 200 }); }) as unknown as typeof fetch;
|
||||
try {
|
||||
const [a, b] = await Promise.all([fetchReportSource("bug", "rs-1"), fetchReportSource("bug", "rs-1")]);
|
||||
const c = await fetchReportSource("bug", "rs-1");
|
||||
expect(calls).toBe(1);
|
||||
expect(a.text).toBe("# report");
|
||||
expect(b.text).toBe("# report");
|
||||
expect(c.text).toBe("# report");
|
||||
// 不同类的同一个 id 是另一份报告(五类路由不共享)。
|
||||
await fetchReportSource("feature", "rs-1");
|
||||
expect(calls).toBe(2);
|
||||
} finally { globalThis.fetch = orig; }
|
||||
});
|
||||
|
||||
test("404 不缓存:报告刚写出来时下一次就能取到", async () => {
|
||||
const orig = globalThis.fetch;
|
||||
let calls = 0;
|
||||
globalThis.fetch = (async () => {
|
||||
calls++;
|
||||
return calls === 1 ? new Response("nope", { status: 404 }) : new Response("# now", { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const first = await fetchReportSource("bug", "rs-404");
|
||||
expect(first.status).toBe(404);
|
||||
expect(first.text).toBeNull();
|
||||
const second = await fetchReportSource("bug", "rs-404");
|
||||
expect(second.text).toBe("# now");
|
||||
expect(calls).toBe(2);
|
||||
} finally { globalThis.fetch = orig; }
|
||||
});
|
||||
});
|
||||
|
||||
describe("fetchEvidenceList", () => {
|
||||
test("在途合并 + 成功复用;失败不缓存", async () => {
|
||||
let calls = 0;
|
||||
const list = async () => { calls++; return { files: [{ name: "a.png", size: 1, mtime: 1, isImage: true }] }; };
|
||||
const [x, y] = await Promise.all([fetchEvidenceList("bug", "ev-1", list), fetchEvidenceList("bug", "ev-1", list)]);
|
||||
await fetchEvidenceList("bug", "ev-1", list);
|
||||
expect(calls).toBe(1);
|
||||
expect(x).toEqual(y);
|
||||
|
||||
let failing = 0;
|
||||
const flaky = async () => { failing++; if (failing === 1) throw new Error("502"); return { files: [] }; };
|
||||
await expect(fetchEvidenceList("bug", "ev-2", flaky)).rejects.toThrow("502");
|
||||
expect(await fetchEvidenceList("bug", "ev-2", flaky)).toEqual([]);
|
||||
expect(failing).toBe(2);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user