|
| 1 | +import { expect, test, type Page } from "@playwright/test"; |
| 2 | + |
| 3 | +const storageKey = "eslint-explorer"; |
| 4 | + |
| 5 | +async function getPersistedJavaScriptCode(page: Page): Promise<string> { |
| 6 | + return page.evaluate(key => { |
| 7 | + const storedValue = window.localStorage.getItem(key); |
| 8 | + |
| 9 | + if (!storedValue) { |
| 10 | + return ""; |
| 11 | + } |
| 12 | + |
| 13 | + return JSON.parse(storedValue).state.code.javascript; |
| 14 | + }, storageKey); |
| 15 | +} |
| 16 | + |
| 17 | +async function getPersistedExplorerState(page: Page): Promise<string> { |
| 18 | + const persistedValue = await page.evaluate( |
| 19 | + key => window.localStorage.getItem(key), |
| 20 | + storageKey, |
| 21 | + ); |
| 22 | + |
| 23 | + if (!persistedValue) { |
| 24 | + throw new Error("Expected explorer state to be persisted"); |
| 25 | + } |
| 26 | + |
| 27 | + return persistedValue; |
| 28 | +} |
| 29 | + |
| 30 | +async function getStoredHashValue(page: Page): Promise<string> { |
| 31 | + return page.evaluate(key => { |
| 32 | + return ( |
| 33 | + new URLSearchParams(window.location.hash.slice(1)).get(key) ?? "" |
| 34 | + ); |
| 35 | + }, storageKey); |
| 36 | +} |
| 37 | + |
| 38 | +async function replaceEditorValue(page: Page, value: string) { |
| 39 | + const codeEditor = page |
| 40 | + .getByRole("region", { name: "Code Editor Panel" }) |
| 41 | + .getByRole("textbox") |
| 42 | + .nth(1); |
| 43 | + |
| 44 | + await codeEditor.click(); |
| 45 | + await codeEditor.press("ControlOrMeta+KeyA"); |
| 46 | + await codeEditor.press("Backspace"); |
| 47 | + await codeEditor.pressSequentially(value); |
| 48 | +} |
| 49 | + |
| 50 | +test("should persist unicode code safely in the URL hash", async ({ page }) => { |
| 51 | + await page.addInitScript(key => { |
| 52 | + window.localStorage.removeItem(key); |
| 53 | + }, storageKey); |
| 54 | + await page.goto("/"); |
| 55 | + |
| 56 | + const unicodeCode = 'const \u03C0 = "\u{1F600}";'; |
| 57 | + |
| 58 | + await replaceEditorValue(page, unicodeCode); |
| 59 | + |
| 60 | + await expect.poll(() => getPersistedJavaScriptCode(page)).toBe(unicodeCode); |
| 61 | + await expect.poll(() => getStoredHashValue(page)).toContain("v2."); |
| 62 | + |
| 63 | + const persistedHash = await page.evaluate(() => window.location.hash); |
| 64 | + |
| 65 | + await page.evaluate(key => { |
| 66 | + window.localStorage.removeItem(key); |
| 67 | + }, storageKey); |
| 68 | + await page.goto(`/${persistedHash}`); |
| 69 | + |
| 70 | + await expect(page.locator(".cm-content")).toContainText(unicodeCode); |
| 71 | +}); |
| 72 | + |
| 73 | +test("should still load state from legacy hash links", async ({ page }) => { |
| 74 | + await page.addInitScript(key => { |
| 75 | + window.localStorage.removeItem(key); |
| 76 | + }, storageKey); |
| 77 | + await page.goto("/"); |
| 78 | + |
| 79 | + const legacyCode = "console.log('legacy hash');"; |
| 80 | + |
| 81 | + await replaceEditorValue(page, legacyCode); |
| 82 | + |
| 83 | + await expect.poll(() => getPersistedJavaScriptCode(page)).toBe(legacyCode); |
| 84 | + |
| 85 | + const persistedValue = await getPersistedExplorerState(page); |
| 86 | + |
| 87 | + const legacyHash = await page.evaluate( |
| 88 | + ([key, value]) => { |
| 89 | + const searchParams = new URLSearchParams(); |
| 90 | + searchParams.set(key, btoa(JSON.stringify(value))); |
| 91 | + return searchParams.toString(); |
| 92 | + }, |
| 93 | + [storageKey, persistedValue], |
| 94 | + ); |
| 95 | + |
| 96 | + await page.evaluate(key => { |
| 97 | + window.localStorage.removeItem(key); |
| 98 | + }, storageKey); |
| 99 | + await page.goto(`/#${legacyHash}`); |
| 100 | + |
| 101 | + await expect(page.locator(".cm-content")).toContainText(legacyCode); |
| 102 | +}); |
| 103 | + |
| 104 | +test("should fall back to localStorage when a v2 hash is malformed", async ({ |
| 105 | + page, |
| 106 | +}) => { |
| 107 | + await page.addInitScript(key => { |
| 108 | + window.localStorage.removeItem(key); |
| 109 | + }, storageKey); |
| 110 | + await page.goto("/"); |
| 111 | + |
| 112 | + const fallbackCode = "console.log('localStorage fallback');"; |
| 113 | + |
| 114 | + await replaceEditorValue(page, fallbackCode); |
| 115 | + |
| 116 | + await expect |
| 117 | + .poll(() => getPersistedJavaScriptCode(page)) |
| 118 | + .toBe(fallbackCode); |
| 119 | + |
| 120 | + const persistedValue = await getPersistedExplorerState(page); |
| 121 | + |
| 122 | + const malformedHash = await page.evaluate(key => { |
| 123 | + const bytes = new TextEncoder().encode("not valid persisted state"); |
| 124 | + let binary = ""; |
| 125 | + |
| 126 | + for (const byte of bytes) { |
| 127 | + binary += String.fromCharCode(byte); |
| 128 | + } |
| 129 | + |
| 130 | + const base64Url = btoa(binary) |
| 131 | + .replace(/\+/g, "-") |
| 132 | + .replace(/\//g, "_") |
| 133 | + .replace(/=+$/, ""); |
| 134 | + |
| 135 | + const searchParams = new URLSearchParams(); |
| 136 | + searchParams.set(key, `v2.${base64Url}`); |
| 137 | + return searchParams.toString(); |
| 138 | + }, storageKey); |
| 139 | + |
| 140 | + await page.evaluate( |
| 141 | + ([key, value]) => { |
| 142 | + window.localStorage.setItem(key, value); |
| 143 | + }, |
| 144 | + [storageKey, persistedValue], |
| 145 | + ); |
| 146 | + await page.goto(`/#${malformedHash}`); |
| 147 | + |
| 148 | + await expect(page.locator(".cm-content")).toContainText(fallbackCode); |
| 149 | +}); |
0 commit comments