Files
cobble/src/utils/last-visited-bot-cookies.test.ts
T
tben 63a93a4262
CI / check (push) Canceled after 0s
Keep only the bot cookie for the last visited bot
A workspace conversation is never remembered, so /admin/bots always
reopens the bot's own chat.
2026-10-22 13:39:36 +01:00

60 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { AppData } from "@/src/data/app-data";
import lastVisitedBotCookies from "./last-visited-bot-cookies";
import { setCookies } from "./cookies";
import readLastVisitedBot from "./read-last-visited-bot";
const bot_cookie = AppData["LastVisitedBotCookieName"];
function requestWithCookies(): Request {
const res = new Response();
setCookies(res, lastVisitedBotCookies({ bot_id: 4, secure: false }));
const cookie_header = res.headers
.getSetCookie()
.map((header) => header.split(";")[0])
.join("; ");
return new Request("http://localhost/admin/bots", {
headers: { Cookie: cookie_header },
});
}
describe("last visited bot cookies", () => {
test("round-trip a bot", () => {
expect(readLastVisitedBot({ req: requestWithCookies() })).toEqual({ bot_id: 4 });
});
test("only the bot cookie is written, never a workspace one", () => {
const res = new Response();
setCookies(res, lastVisitedBotCookies({ bot_id: 4, secure: false }));
const [header, ...others] = res.headers.getSetCookie();
expect(others).toHaveLength(0);
expect(header?.startsWith(`${bot_cookie}=4`)).toBe(true);
});
test("no cookies at all", () => {
expect(readLastVisitedBot({ req: new Request("http://localhost") })).toBeNull();
});
test("a tampered bot id is ignored", () => {
const req = new Request("http://localhost", {
headers: { Cookie: `${bot_cookie}=1%20OR%201=1` },
});
expect(readLastVisitedBot({ req })).toBeNull();
});
test("they are HttpOnly, long-lived and not Secure on plain HTTP", () => {
const res = new Response();
setCookies(
res,
lastVisitedBotCookies({ bot_id: 4, secure: false }),
);
const [header] = res.headers.getSetCookie();
expect(header).toContain("HttpOnly");
expect(header).toContain(`Max-Age=${AppData["LastVisitedCookieMaxAgeSeconds"]}`);
expect(header).not.toContain("Secure");
});
});