Repository navigation
Expand file tree
/
Copy pathbrowserStorageKeys.test.ts
More file actions
72 lines (66 loc) · 2.98 KB
/
Copy pathbrowserStorageKeys.test.ts
File metadata and controls
72 lines (66 loc) · 2.98 KB
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
// SPDX-FileCopyrightText: 2026 Anton Wiklund and Solid Memo contributors
//
// SPDX-License-Identifier: MIT
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { expect, it } from "vitest";
import { isTestFile, sourceFiles } from "./checkBoundaries.ts";
const ROOT = join(import.meta.dirname, "..");
const CONTRACT = join(ROOT, "api/browser-storage");
/**
* The names under `solid-memo` the apps' and packages' code holds that
* are not kept in the browser, each with what it names instead. They
* match whole, so a new storage key that only starts like one of them
* is still checked. A template literal's name ends at its first
* substitution.
*/
const NOT_KEPT: Record<string, string> = {
"solid-memo-answers-": "the file an export of the answers is saved as",
"solid-memo-answers-<study day>.csv": "that file, in a comment",
"solid-memo:client-id": "a build plugin",
"solid-memo:publish-": "a build plugin, one for each published path",
"solid-memo:publish-docs": "a build plugin",
"solid-memo:site-map": "a build plugin",
"solid-memo:session-expired": "an event within the page",
};
/** A string literal's text up to its first substitution, for each literal that starts `solid-memo`. */
const NAME = /["'`](solid-memo[:-][^"'`$]*)/g;
/**
* Whether a line of the contract names `name` whole, followed by a
* comma, a space or the line's end, or names a key it is the prefix of
* (one that ends in a separator).
*/
const listed = (contract: string, name: string) =>
new RegExp(
`: ${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}${/[:.-]$/.test(name) ? "" : "(?=[, \n])"}`,
).test(contract);
/**
* Every name the code keeps in the browser under (a storage key or its
* prefix, a database, a lock, a channel) is in a file of
* api/browser-storage, so that renaming or dropping it is a line gone,
* which `npm run check:breaking` holds to a commit that says so. A key
* only the code knows would change unnoticed and leave what it held
* behind in the browser.
*/
it("finds every browser storage name of the code in api/browser-storage", () => {
const contract = readdirSync(CONTRACT)
.map((file) => readFileSync(join(CONTRACT, file), "utf8"))
.join("");
const names = new Map<string, string>();
for (const parent of ["apps", "packages"]) {
for (const pkg of readdirSync(join(ROOT, parent))) {
const src = join(ROOT, parent, pkg, "src");
for (const file of sourceFiles(src).filter((path) => !isTestFile(path))) {
const text = readFileSync(join(src, file), "utf8");
for (const [, name] of text.matchAll(NAME))
names.set(name!, `${parent}/${pkg}/src/${file}`);
}
}
}
expect(Object.keys(NOT_KEPT).filter((name) => !names.has(name))).toEqual([]);
const kept = [...names.keys()].filter((name) => !Object.hasOwn(NOT_KEPT, name));
expect(kept.length).toBeGreaterThan(0);
expect(
kept.filter((name) => !listed(contract, name)).map((name) => `${name} (${names.get(name)})`),
).toEqual([]);
});