-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb.ts
More file actions
119 lines (108 loc) · 3.87 KB
/
web.ts
File metadata and controls
119 lines (108 loc) · 3.87 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
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
119
import { BrowserContext, Page } from "@playwright/test";
import path from "path";
declare const chrome: {
storage: {
local: {
get: (keys: string[]) => Promise<Record<string, unknown>>;
set: (items: Record<string, unknown>) => void | Promise<void>;
};
};
};
export async function openStableTestPage(page: Page) {
await page.goto("/inspector-playground.html");
await page.waitForLoadState("domcontentloaded");
await page.waitForSelector('[data-testid="playground-main"]');
}
export async function openInspectorPlaygroundPage(page: Page) {
const filePath = path.resolve("e2e/fixtures-pages/inspector-playground.html");
await page.goto(`file://${filePath}`);
await page.waitForLoadState("domcontentloaded");
}
export async function enableDomainInStorage(context: BrowserContext, domain: string) {
let [worker] = context.serviceWorkers();
if (!worker) {
worker = await context.waitForEvent("serviceworker");
}
const setAndVerifyStorage = async (d: string) => {
const toBooleanRecord = (value: unknown): Record<string, boolean> => {
if (typeof value !== "object" || value === null) return {};
const result: Record<string, boolean> = {};
for (const [key, fieldValue] of Object.entries(value)) {
if (typeof fieldValue === "boolean") {
result[key] = fieldValue;
}
}
return result;
};
const toUnknownRecord = (value: unknown): Record<string, unknown> => {
if (typeof value !== "object" || value === null) return {};
const result: Record<string, unknown> = {};
for (const [key, fieldValue] of Object.entries(value)) {
result[key] = fieldValue;
}
return result;
};
const maxAttempts = 5;
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
const result = await chrome.storage.local.get(["amg-state"]);
const currentState = result["amg-state"];
const stateRecord = toUnknownRecord(currentState);
const baseState = {
analytics: toUnknownRecord(stateRecord.analytics),
domains: toBooleanRecord(stateRecord.domains),
votes: toBooleanRecord(stateRecord.votes),
};
await chrome.storage.local.set({
"amg-state": {
...baseState,
domains: {
...baseState.domains,
[d]: true,
},
},
});
const verifyResult = await chrome.storage.local.get(["amg-state"]);
const verifyState = toUnknownRecord(verifyResult["amg-state"]);
const verifyDomains = toBooleanRecord(verifyState.domains);
if (verifyDomains[d] === true) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 50));
}
throw new Error(`Failed to enable domain state for ${d}`);
};
await worker.evaluate(setAndVerifyStorage, domain);
}
export async function enableStableDomainInStorage(context: BrowserContext) {
const domain = "localhost:51234";
await enableDomainInStorage(context, domain);
}
export function getExtensionRoot(page: Page) {
return page.locator("[data-amgiflol-root]");
}
export function getSvelteAppMain(page: Page) {
return page.locator("[data-amgiflol-root] >> main");
}
export function getInspectorActiveMain(page: Page) {
return page.locator("[data-amgiflol-root] >> main.active");
}
export async function expectSvelteAppLoaded(page: Page) {
const timeoutMs = process.env.CI ? 24_000 : 12_000;
const pollMs = 250;
const startTime = Date.now();
const root = getExtensionRoot(page).first();
await root.waitFor({ state: "attached", timeout: timeoutMs });
while (Date.now() - startTime < timeoutMs) {
if (page.isClosed()) {
throw new Error("Page closed while waiting for extension app to activate.");
}
const activeMainCount = await getInspectorActiveMain(page).count();
if (activeMainCount > 0) return;
await page.waitForTimeout(pollMs);
}
const rootCount = await getExtensionRoot(page).count();
const mainCount = await getSvelteAppMain(page).count();
throw new Error(
`Extension app did not activate within ${timeoutMs}ms (rootCount=${rootCount}, mainCount=${mainCount}).`,
);
}