components.test.ts
Raw
1import { expect, test } from "bun:test";
2import { humanReadableTime, ShowFile } from "./components";
3
4const UUID = "00000000-0000-0000-0000-000000000000";
5
6test("escapes filename in the file view (no stored XSS)", async () => {
7 const evil = "<img src=x onerror=alert(1)>";
8 const html = await ShowFile(
9 evil,
10 UUID,
11 new TextEncoder().encode("hi"),
12 "none",
13 null,
14 );
15 expect(html).not.toContain(evil);
16 expect(html).toContain("&lt;img");
17});
18
19test("escapes filename in the encrypted overlay (no stored XSS)", async () => {
20 const evil = "<script>alert(1)</script>";
21 const html = await ShowFile(evil, UUID, null, "none", null);
22 expect(html).not.toContain(evil);
23 expect(html).toContain("&lt;script&gt;");
24});
25
26test("humanReadableTime breaks a total down (no spurious 'expired')", () => {
27 expect(humanReadableTime(4320)).toBe("3 days"); // exact multiple of a day
28 expect(humanReadableTime(120)).toBe("2 hours"); // exact multiple of an hour
29 expect(humanReadableTime(1)).toBe("1 minute");
30 expect(humanReadableTime(1501)).toBe("1 day 1 hour 1 minute");
31 expect(humanReadableTime(0)).toBe("expired");
32 expect(humanReadableTime(-5)).toBe("expired");
33});
34