testSessionsAndCookies
Sessions live on the server, by an id in a cookie; cookies go out with the answer.
import { clearSession, cookie, session, setCookie, setSession, withRequest } from "#codebase/tcmts/server"; import assert from "node:assert/strict"; export const testSessionsAndCookies = async () => { const signIn = await withRequest({}, () => { setSession({ user: "ada" }); setCookie("theme", "dark", { maxAge: 60 }); return session(); }); assert.deepEqual(signIn.value, { user: "ada" }); const sid = /tcm_sid=([^;]+)/.exec(signIn.setCookies.join("\n"))![1]!; assert.match( signIn.setCookies.join("\n"), /theme=dark; Path=\/; SameSite=Lax; Max-Age=60; HttpOnly/, ); const later = await withRequest( { cookies: { tcm_sid: decodeURIComponent(sid), theme: "dark" } }, () => [session(), cookie("theme")], ); assert.deepEqual(later.value, [{ user: "ada" }, "dark"]); const out = await withRequest({ cookies: { tcm_sid: decodeURIComponent(sid) } }, () => { clearSession(); return session(); }); assert.deepEqual(out.value, {}); assert.throws(() => session(), /only works inside a server function/); }