@relate/contrib-web 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +242 -2
- package/conformance/example-site/blank.html +2 -0
- package/conformance/example-site/broken.html +35 -0
- package/conformance/example-site/index.html +35 -0
- package/conformance/example-site/notes.html +35 -0
- package/conformance/example-site/page-1.html +35 -0
- package/conformance/example-site/page-2.html +35 -0
- package/conformance/example-site/public/descriptor-b.json +16 -0
- package/conformance/example-site/public/descriptor-broken.json +16 -0
- package/conformance/example-site/public/descriptor-mismatch.json +16 -0
- package/conformance/example-site/public/descriptor.json +16 -0
- package/conformance/example-site/public/graph-b.json +14408 -0
- package/conformance/example-site/public/graph.json +10808 -0
- package/conformance/example-site/rel-page.ts +147 -0
- package/conformance/example-site/vite.config.ts +23 -0
- package/conformance/make-example.ts +22 -0
- package/conformance/run.ts +152 -0
- package/conformance/scenarios.ts +285 -0
- package/package.json +81 -3
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// The behavior shared by the example pages. It is the contract the conformance check drives: every observable
|
|
2
|
+
// thing is a `data-rel-*` element or attribute, so an adapter's pages only have to produce the same markup.
|
|
3
|
+
//
|
|
4
|
+
// Query parameters: navigation=client-routing|multi-page (the profile), variant=a|b|mismatch|broken (which
|
|
5
|
+
// descriptor), __break=no-rerun (a deliberately broken variant used to test the check itself).
|
|
6
|
+
import engineUrl from "@relate/relate-wasm/relate_bg.wasm?url";
|
|
7
|
+
import { attachView, createSiteClient } from "@relate/contrib-web";
|
|
8
|
+
import type { View } from "@relate/contrib-web";
|
|
9
|
+
|
|
10
|
+
const params = new URLSearchParams(location.search);
|
|
11
|
+
const navigation = (params.get("navigation") ?? "multi-page") as "client-routing" | "multi-page";
|
|
12
|
+
const variant = params.get("variant") ?? document.body.dataset.relVariant ?? "a";
|
|
13
|
+
const descriptors: Record<string, string> = {
|
|
14
|
+
a: "/descriptor.json",
|
|
15
|
+
b: "/descriptor-b.json",
|
|
16
|
+
mismatch: "/descriptor-mismatch.json",
|
|
17
|
+
broken: "/descriptor-broken.json",
|
|
18
|
+
};
|
|
19
|
+
const instance = Math.random().toString(36).slice(2, 8);
|
|
20
|
+
|
|
21
|
+
const client = createSiteClient({
|
|
22
|
+
engineUrl,
|
|
23
|
+
graph: { descriptor: descriptors[variant] ?? "/descriptor.json" },
|
|
24
|
+
navigation,
|
|
25
|
+
issueUrl: "https://example.org/issues",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const trap = `RETURN ${"1+".repeat(200000)}1 AS x`;
|
|
29
|
+
const runaway = "MATCH (a:Page), (b:Page), (c:Page) RETURN count(*) AS c";
|
|
30
|
+
const notes = {
|
|
31
|
+
version: 1 as const,
|
|
32
|
+
nodes: [
|
|
33
|
+
{ id: "note-a", labels: ["Note"] },
|
|
34
|
+
{ id: "note-b", labels: ["Note"] },
|
|
35
|
+
],
|
|
36
|
+
links: [{ source: "note-a", target: "note-b", type: "ABOUT" }],
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const state = { status: client.status, generation: client.generation, count: "…", updates: 0, lastError: "", notes: "…", restored: "unknown", failure: "" };
|
|
40
|
+
const set = (name: string, value: string): void => {
|
|
41
|
+
const el = document.querySelector(`[data-rel-${name}]`);
|
|
42
|
+
if (el) el.textContent = value;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function render(): void {
|
|
46
|
+
set("status", state.status);
|
|
47
|
+
set("generation", String(state.generation));
|
|
48
|
+
set("profile", navigation === "client-routing" ? "persistent" : "per-page");
|
|
49
|
+
set("instance", instance);
|
|
50
|
+
set("restored", state.restored);
|
|
51
|
+
set("count", state.count);
|
|
52
|
+
set("updates", String(state.updates));
|
|
53
|
+
set("last-error", state.lastError);
|
|
54
|
+
set("failure", state.failure);
|
|
55
|
+
set("notes", state.notes);
|
|
56
|
+
// the status as an attribute of <html>, so a scenario can wait for it without matching the status element
|
|
57
|
+
document.documentElement.dataset.relState = state.status;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
client.on("status", ({ status, generation }) => {
|
|
61
|
+
state.status = status;
|
|
62
|
+
state.generation = generation;
|
|
63
|
+
state.failure = status === "failed" ? client.failureMessage() : "";
|
|
64
|
+
render();
|
|
65
|
+
});
|
|
66
|
+
client.on("timing", ({ phase, restored }) => {
|
|
67
|
+
if (phase === "total") {
|
|
68
|
+
state.restored = String(restored);
|
|
69
|
+
render();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
let unsubscribe: Array<() => void> = [];
|
|
74
|
+
let view: View | null = null;
|
|
75
|
+
|
|
76
|
+
function arrive(): void {
|
|
77
|
+
state.count = "…";
|
|
78
|
+
state.notes = "…";
|
|
79
|
+
const notesPage = "relNotesPage" in document.body.dataset;
|
|
80
|
+
document.querySelector("[data-rel-notes-row]")?.toggleAttribute("hidden", !notesPage);
|
|
81
|
+
const off = client.subscribe("MATCH (n:Page) RETURN count(n) AS c", null, (u) => {
|
|
82
|
+
state.status = u.status;
|
|
83
|
+
state.generation = u.generation;
|
|
84
|
+
if (u.rows) {
|
|
85
|
+
state.count = String(u.rows[0]?.c);
|
|
86
|
+
state.updates++;
|
|
87
|
+
if (params.get("__break") === "no-rerun") off(); // a deliberately broken variant: no re-run after a replacement
|
|
88
|
+
}
|
|
89
|
+
render();
|
|
90
|
+
});
|
|
91
|
+
unsubscribe.push(off);
|
|
92
|
+
if (notesPage) {
|
|
93
|
+
// Subscribe once the view's data is in: a subscription made while the data is still being added would
|
|
94
|
+
// show the count before the add, and nothing re-runs it until the next replacement.
|
|
95
|
+
const mine = (view = attachView(client, notes));
|
|
96
|
+
void mine.ready.then(() => {
|
|
97
|
+
if (view !== mine) return; // the page was left before the data arrived
|
|
98
|
+
unsubscribe.push(
|
|
99
|
+
client.subscribe("MATCH (n:Note) RETURN count(n) AS c", null, (u) => {
|
|
100
|
+
if (u.rows) state.notes = String(u.rows[0]?.c);
|
|
101
|
+
render();
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
render();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function depart(): void {
|
|
110
|
+
for (const off of unsubscribe) off();
|
|
111
|
+
unsubscribe = [];
|
|
112
|
+
void view?.detach();
|
|
113
|
+
view = null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
document.addEventListener("click", (e) => {
|
|
117
|
+
const target = e.target as Element | null;
|
|
118
|
+
const action = target?.closest("[data-rel-action]")?.getAttribute("data-rel-action");
|
|
119
|
+
if (action === "trap") {
|
|
120
|
+
void client.query(trap).catch((err: { reason: string }) => ((state.lastError = err.reason), render()));
|
|
121
|
+
} else if (action === "timeout") {
|
|
122
|
+
void client.query(runaway, null, { timeoutMs: 400 }).catch((err: { reason: string }) => ((state.lastError = err.reason), render()));
|
|
123
|
+
} else if (navigation === "client-routing") {
|
|
124
|
+
// Client-side routing: fetch the next page and swap its <main>, so this module (and the client) stays alive.
|
|
125
|
+
const a = target?.closest("a[data-rel-link]") as HTMLAnchorElement | null;
|
|
126
|
+
if (a && !e.defaultPrevented && e.button === 0 && !e.metaKey && !e.ctrlKey && !e.shiftKey) {
|
|
127
|
+
e.preventDefault();
|
|
128
|
+
void go(a.href, true);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
async function go(href: string, push: boolean): Promise<void> {
|
|
134
|
+
const html = await (await fetch(href)).text();
|
|
135
|
+
const next = new DOMParser().parseFromString(html, "text/html");
|
|
136
|
+
depart();
|
|
137
|
+
document.querySelector("main")!.innerHTML = next.querySelector("main")!.innerHTML;
|
|
138
|
+
document.title = next.title;
|
|
139
|
+
document.body.removeAttribute("data-rel-notes-page");
|
|
140
|
+
if (next.body.hasAttribute("data-rel-notes-page")) document.body.setAttribute("data-rel-notes-page", "");
|
|
141
|
+
if (push) history.pushState({}, "", href);
|
|
142
|
+
arrive();
|
|
143
|
+
}
|
|
144
|
+
if (navigation === "client-routing") window.addEventListener("popstate", () => void go(location.href, false));
|
|
145
|
+
if (navigation === "multi-page") window.addEventListener("pagehide", depart);
|
|
146
|
+
|
|
147
|
+
arrive();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { readdirSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
6
|
+
const repoRoot = fileURLToPath(new URL("../../../../", import.meta.url));
|
|
7
|
+
|
|
8
|
+
// Multi-page build of the plain example pages: every .html file in this directory is an entry.
|
|
9
|
+
const input = Object.fromEntries(
|
|
10
|
+
readdirSync(here)
|
|
11
|
+
.filter((f) => f.endsWith(".html"))
|
|
12
|
+
.map((f) => [f.replace(/\.html$/, ""), fileURLToPath(new URL(f, import.meta.url))]),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
root: here,
|
|
17
|
+
// Adapters import the package by name; here the name points at the package's own build output.
|
|
18
|
+
resolve: { alias: { "@relate/contrib-web": fileURLToPath(new URL("../../dist/index.js", import.meta.url)) } },
|
|
19
|
+
server: { fs: { allow: [repoRoot] } },
|
|
20
|
+
worker: { format: "es" },
|
|
21
|
+
build: { target: "es2022", rollupOptions: { input } },
|
|
22
|
+
optimizeDeps: { exclude: ["@relate/relate-wasm"] },
|
|
23
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Writes the example site's data: two variants of a small graph of pages (a and b, different revisions and sizes),
|
|
2
|
+
// a descriptor whose counts do not match its data, and a descriptor whose data cannot be fetched.
|
|
3
|
+
// Run: pnpm exec tsx conformance/make-example.ts
|
|
4
|
+
import { writeFileSync } from "node:fs";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { generateGraph } from "../tests/fixtures/generate-graph.js";
|
|
7
|
+
|
|
8
|
+
const out = fileURLToPath(new URL("./example-site/public/", import.meta.url));
|
|
9
|
+
const write = (name: string, value: unknown): void => writeFileSync(out + name, JSON.stringify(value, null, 1) + "\n");
|
|
10
|
+
|
|
11
|
+
const a = generateGraph(300, { id: "example", revision: "example-a", dataUrl: "/graph.json" });
|
|
12
|
+
const b = generateGraph(400, { id: "example", revision: "example-b", dataUrl: "/graph-b.json" });
|
|
13
|
+
|
|
14
|
+
write("graph.json", a.document);
|
|
15
|
+
write("descriptor.json", a.descriptor);
|
|
16
|
+
write("graph-b.json", b.document);
|
|
17
|
+
write("descriptor-b.json", b.descriptor);
|
|
18
|
+
// The data is variant a's, but the descriptor claims one node fewer: the two disagree.
|
|
19
|
+
write("descriptor-mismatch.json", { ...a.descriptor, data: { ...a.descriptor.data, nodes: a.descriptor.data.nodes - 1 } });
|
|
20
|
+
// The data location does not exist: every attempt to load fails.
|
|
21
|
+
write("descriptor-broken.json", { ...a.descriptor, id: "example-broken", data: { ...a.descriptor.data, url: "/missing.json" } });
|
|
22
|
+
console.log("example data written to", out);
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// The conformance check: runs the framework-neutral scenarios against a site that serves the example pages.
|
|
2
|
+
// An adapter serves the same pages through its framework and calls `runConformance` for each profile and browser.
|
|
3
|
+
// Needs Playwright, an optional peer dependency of the package.
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { chromium, firefox, webkit, type Browser, type BrowserContext } from "@playwright/test";
|
|
9
|
+
import { scenarios, type BrowserName, type Profile, type Scenario } from "./scenarios.js";
|
|
10
|
+
|
|
11
|
+
export { scenarios } from "./scenarios.js";
|
|
12
|
+
export type { BrowserName, Profile, Scenario } from "./scenarios.js";
|
|
13
|
+
|
|
14
|
+
const publicDir = [new URL("./example-site/public/", import.meta.url), new URL("../../conformance/example-site/public/", import.meta.url)].find((u) => existsSync(new URL("descriptor.json", u)));
|
|
15
|
+
const read = (name: string): unknown => JSON.parse(readFileSync(new URL(name, publicDir), "utf8"));
|
|
16
|
+
|
|
17
|
+
/** The example site's descriptor and data document (variant a). */
|
|
18
|
+
export const exampleDescriptor = read("descriptor.json") as { version: 1; id: string; revision: string; data: { url: string; nodes: number; links: number }; indexes?: Array<{ label: string; property: string }> };
|
|
19
|
+
export const exampleDocument = read("graph.json") as { version: 1; revision?: string; nodes: unknown[]; links: unknown[] };
|
|
20
|
+
|
|
21
|
+
export interface RunOptions {
|
|
22
|
+
/** Where the example pages are served, for example `http://localhost:5175`. */
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
profile: Profile;
|
|
25
|
+
browser: BrowserName;
|
|
26
|
+
/** Extra query string for every page (used to run a deliberately broken variant of the pages). */
|
|
27
|
+
query?: string;
|
|
28
|
+
/**
|
|
29
|
+
* How to recognize a request that fetched variant a's or variant b's data. Optional: by default the example site's
|
|
30
|
+
* file names (`/graph.json`, `/graph-b.json`) are matched. An adapter whose data URLs are named differently passes its own.
|
|
31
|
+
*/
|
|
32
|
+
dataRequests?: { a: (url: string) => boolean; b: (url: string) => boolean };
|
|
33
|
+
/** Run only these scenarios. */
|
|
34
|
+
only?: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ScenarioResult {
|
|
38
|
+
id: string;
|
|
39
|
+
title: string;
|
|
40
|
+
spec: string[];
|
|
41
|
+
status: "passed" | "failed" | "skipped";
|
|
42
|
+
/** For a failure: the behavior that is broken. */
|
|
43
|
+
behavior?: string;
|
|
44
|
+
error?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Report {
|
|
48
|
+
profile: Profile;
|
|
49
|
+
browser: BrowserName;
|
|
50
|
+
results: ScenarioResult[];
|
|
51
|
+
failures: ScenarioResult[];
|
|
52
|
+
passed: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const launchers = { chromium, firefox, webkit };
|
|
56
|
+
|
|
57
|
+
/** Empty the origin-private file system so that one scenario's stores do not reach the next (see the test fixtures). */
|
|
58
|
+
async function clearOpfs(context: BrowserContext, baseUrl: string): Promise<void> {
|
|
59
|
+
const page = await context.newPage();
|
|
60
|
+
try {
|
|
61
|
+
await page.goto(`${baseUrl}/blank.html`);
|
|
62
|
+
for (let attempt = 0; attempt < 25; attempt++) {
|
|
63
|
+
const left = await page.evaluate(async () => {
|
|
64
|
+
const root = await navigator.storage.getDirectory();
|
|
65
|
+
const names: string[] = [];
|
|
66
|
+
for await (const [name] of (root as any).entries()) names.push(name);
|
|
67
|
+
for (const name of names) await root.removeEntry(name, { recursive: true }).catch(() => undefined);
|
|
68
|
+
let remaining = 0;
|
|
69
|
+
for await (const _ of (root as any).entries()) remaining++;
|
|
70
|
+
return remaining;
|
|
71
|
+
});
|
|
72
|
+
if (left === 0) return;
|
|
73
|
+
await page.waitForTimeout(400);
|
|
74
|
+
}
|
|
75
|
+
} finally {
|
|
76
|
+
await page.close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function withContext<T>(options: RunOptions, shared: { browser: Browser | null }, use: (context: BrowserContext) => Promise<T>): Promise<T> {
|
|
81
|
+
if (options.browser !== "webkit") {
|
|
82
|
+
const context = await shared.browser!.newContext();
|
|
83
|
+
try {
|
|
84
|
+
return await use(context);
|
|
85
|
+
} finally {
|
|
86
|
+
await context.close();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// WebKit needs a persistent profile for the origin-private file system.
|
|
90
|
+
const profile = mkdtempSync(join(tmpdir(), "relate-conformance-"));
|
|
91
|
+
const context = await webkit.launchPersistentContext(profile);
|
|
92
|
+
try {
|
|
93
|
+
await clearOpfs(context, options.baseUrl);
|
|
94
|
+
return await use(context);
|
|
95
|
+
} finally {
|
|
96
|
+
await Promise.all(context.pages().map((p) => p.close().catch(() => undefined)));
|
|
97
|
+
await clearOpfs(context, options.baseUrl).catch(() => undefined);
|
|
98
|
+
await context.close();
|
|
99
|
+
rmSync(profile, { recursive: true, force: true });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function runConformance(options: RunOptions): Promise<Report> {
|
|
104
|
+
const selected = scenarios.filter((s) => !options.only || options.only.includes(s.id));
|
|
105
|
+
const shared: { browser: Browser | null } = { browser: options.browser === "webkit" ? null : await launchers[options.browser].launch() };
|
|
106
|
+
const results: ScenarioResult[] = [];
|
|
107
|
+
try {
|
|
108
|
+
for (const scenario of selected) {
|
|
109
|
+
const base = { id: scenario.id, title: scenario.title, spec: scenario.spec };
|
|
110
|
+
if (!scenario.profiles.includes(options.profile)) {
|
|
111
|
+
results.push({ ...base, status: "skipped" });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
await withContext(options, shared, async (context) => {
|
|
116
|
+
await scenario.run({
|
|
117
|
+
context,
|
|
118
|
+
baseUrl: options.baseUrl,
|
|
119
|
+
profile: options.profile,
|
|
120
|
+
browser: options.browser,
|
|
121
|
+
isDataA: options.dataRequests?.a ?? ((url) => url.includes("/graph.json")),
|
|
122
|
+
isDataB: options.dataRequests?.b ?? ((url) => url.includes("/graph-b.json")),
|
|
123
|
+
async open(path, extra, requests) {
|
|
124
|
+
const page = await context.newPage();
|
|
125
|
+
if (requests) page.on("request", (r) => requests.push(r.url()));
|
|
126
|
+
const query = [`navigation=${options.profile}`, extra, options.query].filter(Boolean).join("&");
|
|
127
|
+
await page.goto(`${options.baseUrl}${path}?${query}`);
|
|
128
|
+
return page;
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
results.push({ ...base, status: "passed" });
|
|
133
|
+
} catch (e) {
|
|
134
|
+
results.push({ ...base, status: "failed", behavior: scenario.behavior, error: e instanceof Error ? e.message : String(e) });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} finally {
|
|
138
|
+
await shared.browser?.close();
|
|
139
|
+
}
|
|
140
|
+
const failures = results.filter((r) => r.status === "failed");
|
|
141
|
+
return { profile: options.profile, browser: options.browser, results, failures, passed: failures.length === 0 };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A report as text, naming each failed scenario's broken behavior. */
|
|
145
|
+
export function formatReport(report: Report): string {
|
|
146
|
+
const lines = [`conformance: ${report.browser}, ${report.profile}: ${report.passed ? "passed" : "FAILED"}`];
|
|
147
|
+
for (const r of report.results) {
|
|
148
|
+
lines.push(` ${r.status.padEnd(7)} ${r.id} ${r.title}`);
|
|
149
|
+
if (r.status === "failed") lines.push(` broken behavior: ${r.behavior}`, ` ${r.error}`);
|
|
150
|
+
}
|
|
151
|
+
return lines.join("\n");
|
|
152
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// Framework-neutral conformance scenarios (specs contracts/conformance.md). They drive the example pages by what a
|
|
2
|
+
// visitor can see: `data-rel-*` elements and attributes, links, buttons and the network. They know nothing about
|
|
3
|
+
// the framework that serves the pages, so an adapter passes by producing the same markup and behavior.
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import type { BrowserContext, Page } from "@playwright/test";
|
|
6
|
+
|
|
7
|
+
export type Profile = "client-routing" | "multi-page";
|
|
8
|
+
export type BrowserName = "chromium" | "firefox" | "webkit";
|
|
9
|
+
|
|
10
|
+
export interface ScenarioContext {
|
|
11
|
+
context: BrowserContext;
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
profile: Profile;
|
|
14
|
+
browser: BrowserName;
|
|
15
|
+
/** Whether a request URL fetched variant a's data, and variant b's. The example site's file names by default. */
|
|
16
|
+
isDataA(url: string): boolean;
|
|
17
|
+
isDataB(url: string): boolean;
|
|
18
|
+
/** Open a page of the example site, with the scenario's profile in its query. `requests` collects its request URLs. */
|
|
19
|
+
open(path: string, extra?: string, requests?: string[]): Promise<Page>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface Scenario {
|
|
23
|
+
id: string;
|
|
24
|
+
title: string;
|
|
25
|
+
/** The requirements and user-story scenarios this one checks. */
|
|
26
|
+
spec: string[];
|
|
27
|
+
/** The behavior that is broken when this scenario fails, in words an adapter author can act on. */
|
|
28
|
+
behavior: string;
|
|
29
|
+
profiles: Profile[];
|
|
30
|
+
run(ctx: ScenarioContext): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const BOTH: Profile[] = ["client-routing", "multi-page"];
|
|
34
|
+
const PAGES = "300"; // variant a of the example graph
|
|
35
|
+
const PAGES_B = "400"; // variant b
|
|
36
|
+
|
|
37
|
+
const text = async (page: Page, name: string): Promise<string> => ((await page.locator(`[data-rel-${name}]`).first().textContent()) ?? "").trim();
|
|
38
|
+
const settled = (page: Page, status = "ready", timeout = 60_000) =>
|
|
39
|
+
page.waitForFunction((s) => document.documentElement.dataset.relState === s, status, { timeout });
|
|
40
|
+
const showing = (page: Page, name: string, value: string, timeout = 30_000) =>
|
|
41
|
+
page.waitForFunction(([n, v]) => document.querySelector(`[data-rel-${n}]`)?.textContent?.trim() === v, [name, value] as const, { timeout });
|
|
42
|
+
|
|
43
|
+
/** Click a link and wait until the page it leads to is showing (works for a client-side swap and a full load). */
|
|
44
|
+
async function follow(page: Page, label: string, title: string): Promise<void> {
|
|
45
|
+
await page.locator(`a[data-rel-link]`, { hasText: label }).first().click();
|
|
46
|
+
await page.waitForFunction((t) => document.querySelector("[data-rel-title]")?.textContent?.trim() === t, title);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const scenarios: Scenario[] = [
|
|
50
|
+
{
|
|
51
|
+
id: "C-01",
|
|
52
|
+
title: "a per-page query returns what the data implies, and links work before the graph is ready",
|
|
53
|
+
spec: ["US1", "FR-006", "FR-003"],
|
|
54
|
+
behavior: "a page's first render must not depend on the instance, and a subscription delivers the query's rows",
|
|
55
|
+
profiles: BOTH,
|
|
56
|
+
async run(ctx) {
|
|
57
|
+
const raw = await (await ctx.context.request.get(`${ctx.baseUrl}/page-1.html`)).text();
|
|
58
|
+
assert.match(raw, /data-rel-link/, "the navigation links are in the page as served, before any script runs");
|
|
59
|
+
assert.match(raw, /data-rel-status/, "the status placeholder is in the page as served");
|
|
60
|
+
const page = await ctx.open("/page-1.html");
|
|
61
|
+
await settled(page);
|
|
62
|
+
assert.equal(await text(page, "count"), PAGES, "the query result is shown");
|
|
63
|
+
assert.ok((await page.locator("a[data-rel-link]").count()) >= 4, "the links are present");
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "C-02",
|
|
68
|
+
title: "the shared graph is visible from several pages without being loaded again",
|
|
69
|
+
spec: ["US4", "US5", "FR-020", "FR-021"],
|
|
70
|
+
behavior: "the graph must live as long as the profile allows: one instance per visit (client-routing) or a restore from the store (multi-page)",
|
|
71
|
+
profiles: BOTH,
|
|
72
|
+
async run(ctx) {
|
|
73
|
+
if (ctx.profile === "client-routing") {
|
|
74
|
+
const page = await ctx.open("/index.html");
|
|
75
|
+
await settled(page);
|
|
76
|
+
const instance = await text(page, "instance");
|
|
77
|
+
for (const [label, title] of [["Page 1", "Example site: page 1"], ["Page 2", "Example site: page 2"], ["Notes", "Example site: notes"], ["Home", "Example site: home"]] as const) {
|
|
78
|
+
await follow(page, label, title);
|
|
79
|
+
await showing(page, "count", PAGES);
|
|
80
|
+
assert.equal(await text(page, "instance"), instance, `the same instance serves ${label}`);
|
|
81
|
+
assert.equal(await text(page, "generation"), "1", `the generation is unchanged at ${label}`);
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
const first = await ctx.open("/page-1.html");
|
|
85
|
+
await settled(first);
|
|
86
|
+
assert.equal(await text(first, "restored"), "false", "the first page loads the graph");
|
|
87
|
+
await first.close();
|
|
88
|
+
const requests: string[] = [];
|
|
89
|
+
const second = await ctx.open("/page-2.html", undefined, requests);
|
|
90
|
+
await settled(second);
|
|
91
|
+
assert.equal(await text(second, "restored"), "true", "the second page restores the graph from the store");
|
|
92
|
+
assert.equal(await text(second, "count"), PAGES);
|
|
93
|
+
assert.deepEqual(requests.filter((u) => ctx.isDataA(u)), [], "the data document is not requested again");
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: "C-03",
|
|
99
|
+
title: "a graph that cannot load ends in `failed`, and the page's links still work",
|
|
100
|
+
spec: ["US3", "FR-033", "FR-034"],
|
|
101
|
+
behavior: "after three failed attempts the status must be `failed`, with the failure wording shown and the page usable",
|
|
102
|
+
profiles: BOTH,
|
|
103
|
+
async run(ctx) {
|
|
104
|
+
const page = await ctx.open("/broken.html");
|
|
105
|
+
await settled(page, "failed", 90_000);
|
|
106
|
+
assert.match(await text(page, "failure"), /3 attempts/, "the failure wording says how many attempts were made");
|
|
107
|
+
assert.match(await text(page, "failure"), /report this as an issue/, "the wording suggests reporting an issue");
|
|
108
|
+
await follow(page, "Home", "Example site: home");
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
id: "C-04",
|
|
113
|
+
title: "a crash is recovered from: the instance is replaced and subscriptions re-run",
|
|
114
|
+
spec: ["US3", "FR-009", "FR-004"],
|
|
115
|
+
behavior: "subscriptions re-run after a replacement, and the page is not reloaded",
|
|
116
|
+
profiles: BOTH,
|
|
117
|
+
async run(ctx) {
|
|
118
|
+
const page = await ctx.open("/page-1.html");
|
|
119
|
+
await settled(page);
|
|
120
|
+
await page.evaluate(() => ((window as unknown as { __kept: boolean }).__kept = true));
|
|
121
|
+
const before = Number(await text(page, "updates"));
|
|
122
|
+
await page.locator('[data-rel-action="trap"]').click();
|
|
123
|
+
await showing(page, "generation", "2", 60_000);
|
|
124
|
+
await settled(page);
|
|
125
|
+
await page.waitForFunction((n) => Number(document.querySelector("[data-rel-updates]")?.textContent) > n, before, { timeout: 30_000 });
|
|
126
|
+
assert.equal(await text(page, "last-error"), "trap");
|
|
127
|
+
assert.equal(await page.evaluate(() => (window as unknown as { __kept?: boolean }).__kept), true, "the page was not reloaded");
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: "C-05",
|
|
132
|
+
title: "a statement that exceeds its timeout fails with `timeout` and the instance is replaced",
|
|
133
|
+
spec: ["US3", "US6", "FR-009"],
|
|
134
|
+
behavior: "a call's timeout must reject it with `timeout` and replace the instance",
|
|
135
|
+
profiles: BOTH,
|
|
136
|
+
async run(ctx) {
|
|
137
|
+
const page = await ctx.open("/page-1.html");
|
|
138
|
+
await settled(page);
|
|
139
|
+
await page.locator('[data-rel-action="timeout"]').click();
|
|
140
|
+
await showing(page, "generation", "2", 60_000);
|
|
141
|
+
await settled(page);
|
|
142
|
+
assert.equal(await text(page, "last-error"), "timeout");
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: "C-06",
|
|
147
|
+
title: "a page's own data is added on arrival, dropped on departure and added again after a replacement",
|
|
148
|
+
spec: ["US4", "FR-026"],
|
|
149
|
+
behavior: "page-scoped data must be keyed to the page view: added on arrival, dropped on departure, re-added after a replacement",
|
|
150
|
+
profiles: BOTH,
|
|
151
|
+
async run(ctx) {
|
|
152
|
+
const page = await ctx.open("/notes.html");
|
|
153
|
+
await settled(page);
|
|
154
|
+
await showing(page, "notes", "2");
|
|
155
|
+
await page.locator('[data-rel-action="trap"]').click();
|
|
156
|
+
await showing(page, "generation", "2", 60_000);
|
|
157
|
+
await showing(page, "notes", "2", 60_000);
|
|
158
|
+
if (ctx.profile === "client-routing") {
|
|
159
|
+
await follow(page, "Home", "Example site: home");
|
|
160
|
+
await follow(page, "Notes", "Example site: notes");
|
|
161
|
+
await showing(page, "notes", "2");
|
|
162
|
+
assert.equal(await page.evaluate(async () => 0), 0);
|
|
163
|
+
} else {
|
|
164
|
+
await page.reload();
|
|
165
|
+
await settled(page);
|
|
166
|
+
await showing(page, "notes", "2");
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
id: "C-07",
|
|
172
|
+
title: "a changed revision replaces the stored graph and shows none of the old data",
|
|
173
|
+
spec: ["US5", "FR-022", "FR-038", "SC-013"],
|
|
174
|
+
behavior: "staleness must be decided by the descriptor's revision against the revision in the cache's catalog entry, and a matching revision must not fetch the data",
|
|
175
|
+
profiles: ["multi-page"],
|
|
176
|
+
async run(ctx) {
|
|
177
|
+
const a = await ctx.open("/page-1.html");
|
|
178
|
+
await settled(a);
|
|
179
|
+
assert.equal(await text(a, "count"), PAGES);
|
|
180
|
+
await a.close();
|
|
181
|
+
const requests: string[] = [];
|
|
182
|
+
const b = await ctx.open("/page-1.html", "variant=b", requests);
|
|
183
|
+
await settled(b);
|
|
184
|
+
assert.equal(await text(b, "restored"), "false", "a new revision is loaded, not restored");
|
|
185
|
+
assert.equal(await text(b, "count"), PAGES_B, "only the new data is shown");
|
|
186
|
+
assert.ok(requests.some((u) => ctx.isDataB(u)), "the new data was fetched");
|
|
187
|
+
await b.close();
|
|
188
|
+
const again: string[] = [];
|
|
189
|
+
const c = await ctx.open("/page-1.html", "variant=b", again);
|
|
190
|
+
await settled(c);
|
|
191
|
+
assert.equal(await text(c, "restored"), "true", "the same revision is restored");
|
|
192
|
+
assert.deepEqual(again.filter((u) => ctx.isDataB(u)), [], "and the data is not fetched");
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: "C-08",
|
|
197
|
+
title: "a store held by another page falls back to an in-memory graph",
|
|
198
|
+
spec: ["US5", "FR-012", "SC-007"],
|
|
199
|
+
behavior: "a locked store must be retried for at most five seconds, then the page continues with an in-memory graph",
|
|
200
|
+
profiles: ["multi-page"],
|
|
201
|
+
async run(ctx) {
|
|
202
|
+
const holder = await ctx.open("/page-1.html");
|
|
203
|
+
await settled(holder);
|
|
204
|
+
const started = Date.now();
|
|
205
|
+
const second = await ctx.open("/page-2.html");
|
|
206
|
+
await settled(second, "ready", 60_000);
|
|
207
|
+
const elapsed = Date.now() - started;
|
|
208
|
+
assert.equal(await text(second, "restored"), "false", "the second page could not use the held store");
|
|
209
|
+
assert.equal(await text(second, "count"), PAGES);
|
|
210
|
+
assert.ok(elapsed >= 4500 && elapsed < 11_000, `it waited about five seconds before falling back (${elapsed} ms)`);
|
|
211
|
+
await second.close();
|
|
212
|
+
await holder.close();
|
|
213
|
+
const third = await ctx.open("/page-1.html");
|
|
214
|
+
await settled(third);
|
|
215
|
+
assert.equal(await text(third, "restored"), "true", "the held store was left intact");
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
id: "C-09",
|
|
220
|
+
title: "the configured profile decides",
|
|
221
|
+
spec: ["FR-028"],
|
|
222
|
+
behavior: "the `navigation` configuration value must decide the profile",
|
|
223
|
+
profiles: BOTH,
|
|
224
|
+
async run(ctx) {
|
|
225
|
+
const own = await ctx.open("/page-1.html");
|
|
226
|
+
await settled(own);
|
|
227
|
+
assert.equal(await text(own, "profile"), ctx.profile === "client-routing" ? "persistent" : "per-page");
|
|
228
|
+
await own.close();
|
|
229
|
+
const otherProfile = ctx.profile === "client-routing" ? "multi-page" : "client-routing";
|
|
230
|
+
const other = await ctx.context.newPage();
|
|
231
|
+
await other.goto(`${ctx.baseUrl}/page-1.html?navigation=${otherProfile}`);
|
|
232
|
+
await settled(other);
|
|
233
|
+
assert.equal(await text(other, "profile"), otherProfile === "client-routing" ? "persistent" : "per-page");
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: "C-10",
|
|
238
|
+
title: "data that disagrees with its descriptor is rejected and nothing is loaded",
|
|
239
|
+
spec: ["FR-039"],
|
|
240
|
+
behavior: "the loader must check the data's counts and revision against the descriptor and load nothing on a mismatch",
|
|
241
|
+
profiles: BOTH,
|
|
242
|
+
async run(ctx) {
|
|
243
|
+
const page = await ctx.open("/page-1.html", "variant=mismatch");
|
|
244
|
+
await settled(page, "failed", 90_000);
|
|
245
|
+
assert.notEqual(await text(page, "count"), PAGES, "no data was loaded");
|
|
246
|
+
assert.match(await text(page, "failure"), /mismatch|nodes/, "the failure says what disagreed");
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
id: "C-11",
|
|
251
|
+
title: "a store emptied while its catalog entry remains is treated as stale",
|
|
252
|
+
spec: ["US5", "FR-038"],
|
|
253
|
+
behavior: "the node count of a restored store must be checked against the descriptor's, and a difference treated as stale",
|
|
254
|
+
profiles: ["multi-page"],
|
|
255
|
+
async run(ctx) {
|
|
256
|
+
const first = await ctx.open("/page-1.html");
|
|
257
|
+
await settled(first);
|
|
258
|
+
await first.close();
|
|
259
|
+
const tool = await ctx.context.newPage();
|
|
260
|
+
await tool.goto(`${ctx.baseUrl}/blank.html`);
|
|
261
|
+
const deadline = Date.now() + 30_000;
|
|
262
|
+
let removed = "";
|
|
263
|
+
while (Date.now() < deadline && removed !== "removed") {
|
|
264
|
+
removed = await tool.evaluate(async () => {
|
|
265
|
+
try {
|
|
266
|
+
const root = await navigator.storage.getDirectory();
|
|
267
|
+
await (await root.getDirectoryHandle("relate")).removeEntry("rel-example", { recursive: true });
|
|
268
|
+
return "removed";
|
|
269
|
+
} catch (e) {
|
|
270
|
+
return String(e);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
if (removed !== "removed") await tool.waitForTimeout(500);
|
|
274
|
+
}
|
|
275
|
+
assert.equal(removed, "removed", "the test could not empty the store");
|
|
276
|
+
await tool.close();
|
|
277
|
+
const requests: string[] = [];
|
|
278
|
+
const again = await ctx.open("/page-1.html", undefined, requests);
|
|
279
|
+
await settled(again);
|
|
280
|
+
assert.equal(await text(again, "restored"), "false", "an emptied store is not restored");
|
|
281
|
+
assert.equal(await text(again, "count"), PAGES);
|
|
282
|
+
assert.ok(requests.some((u) => ctx.isDataA(u)), "the data was fetched again");
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
];
|