| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { DEFAULT_THEME, THEME_CHOICES, THEME_COOKIE, readTheme, resolvedTheme, themeAttribute, themeCookie } from "./theme.ts"; |
| 5 | |
| 6 | test("the cookie keeps auto, light or dark, and anything else is the default", () => { |
| 7 | for (const choice of THEME_CHOICES) assert.equal(readTheme(choice), choice); |
| 8 | for (const junk of [null, undefined, "", "LIGHT", "sepia", "dark; Path=/"]) assert.equal(readTheme(junk), DEFAULT_THEME); |
| 9 | }); |
| 10 | |
| 11 | test("the cookie is kept a year, on every path, and secure on https", () => { |
| 12 | assert.equal(themeCookie("light", true), `${THEME_COOKIE}=light; Path=/; Max-Age=31536000; SameSite=Lax; Secure`); |
| 13 | assert.equal(themeCookie("auto", false), `${THEME_COOKIE}=auto; Path=/; Max-Age=31536000; SameSite=Lax`); |
| 14 | }); |
| 15 | |
| 16 | test("Light and Dark are data-theme on <html>; Auto leaves it to the system", () => { |
| 17 | assert.equal(themeAttribute("light"), "light"); |
| 18 | assert.equal(themeAttribute("dark"), "dark"); |
| 19 | assert.equal(themeAttribute("auto"), undefined); |
| 20 | }); |
| 21 | |
| 22 | test("a chosen theme is itself; Auto with no browser to ask is dark", () => { |
| 23 | assert.equal(resolvedTheme("light"), "light"); |
| 24 | assert.equal(resolvedTheme("dark"), "dark"); |
| 25 | assert.equal(resolvedTheme("auto"), "dark"); |
| 26 | }); |