1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
import assert from "node:assert/strict";
import { test } from "node:test";
import vm from "node:vm";
import { expireShadowingCookies } from "../lib/shared-cookies.ts";
import { themeBootstrap, themeCookie, themeFromCookies } from "../lib/theme.ts";
test("the theme comes from its cookie and nothing else", () => {
assert.equal(themeFromCookies("progmasoft_theme=dark"), "dark");
assert.equal(themeFromCookies("a=1; progmasoft_theme=light; b=2"), "light");
assert.equal(themeFromCookies("progmasoft_theme=blue"), undefined);
assert.equal(themeFromCookies("my_progmasoft_theme=dark"), undefined);
assert.equal(themeFromCookies(""), undefined);
});
test("the theme cookie is shared by the Progmasoft hosts and secure over HTTPS", () => {
assert.equal(
themeCookie("dark", "viget.progmasoft.com", true),
"progmasoft_theme=dark; Path=/; Max-Age=31536000; SameSite=Lax; Domain=.progmasoft.com; Secure",
);
assert.equal(
themeCookie("light", "progmasoft.com", false),
"progmasoft_theme=light; Path=/; Max-Age=31536000; SameSite=Lax; Domain=.progmasoft.com",
);
assert.equal(
themeCookie("light", "localhost", false),
"progmasoft_theme=light; Path=/; Max-Age=31536000; SameSite=Lax",
);
assert.equal(
themeCookie("dark", "notprogmasoft.com", true),
"progmasoft_theme=dark; Path=/; Max-Age=31536000; SameSite=Lax; Secure",
);
});
/** Runs the bootstrap in a page with the given cookie, localStorage and address. */
function boot({
cookie = "",
stored = null,
host = "account.progmasoft.com",
protocol = "https:",
}) {
const written = [];
const root = { dataset: {}, style: {} };
const document = {
documentElement: root,
get cookie() {
return cookie;
},
set cookie(value) {
written.push(value);
},
};
const localStorage = {
getItem: (name) => (name === "progmasoft_theme" ? stored : null),
};
vm.runInNewContext(themeBootstrap, {
document,
localStorage,
location: { hostname: host, protocol },
});
return { theme: root.dataset.theme, scheme: root.style.colorScheme, written };
}
test("the page starts in the theme of the cookie", () => {
assert.deepEqual(boot({ cookie: "x=1; progmasoft_theme=dark" }), {
theme: "dark",
scheme: "dark",
written: [],
});
});
test("a choice of an earlier version moves from localStorage into the shared cookie", () => {
const result = boot({ stored: "dark" });
assert.equal(result.theme, "dark");
assert.deepEqual(result.written, [
themeCookie("dark", "account.progmasoft.com", true),
]);
assert.deepEqual(
boot({ stored: "light", host: "localhost", protocol: "http:" }).written,
[themeCookie("light", "localhost", false)],
);
});
test("the cookie wins over localStorage, and light is the default", () => {
assert.equal(
boot({ cookie: "progmasoft_theme=light", stored: "dark" }).theme,
"light",
);
assert.equal(boot({}).theme, "light");
assert.equal(boot({ stored: "blue" }).theme, "light");
});
test("a host cookie that shadows a shared one is expired", () => {
assert.deepEqual(
expireShadowingCookies(
"progmasoft_locale=en-US; progmasoft_locale=de-DE",
true,
),
["progmasoft_locale=; Path=/; Max-Age=0; SameSite=Lax; Secure"],
);
assert.deepEqual(
expireShadowingCookies(
"progmasoft_theme=light; a=1; progmasoft_theme=dark",
false,
),
["progmasoft_theme=; Path=/; Max-Age=0; SameSite=Lax"],
);
assert.deepEqual(
expireShadowingCookies(
"progmasoft_locale=de-DE; progmasoft_theme=dark",
true,
),
[],
);
assert.deepEqual(expireShadowingCookies("", true), []);
});
|