|
| 1 | +import { expect, test, type Page } from "@playwright/test"; |
| 2 | +import { fillCommandForm, saveCommandDialog, verifySuccessToast } from "./helpers/test-helpers"; |
| 3 | + |
| 4 | +const STORAGE_KEY_PREFIX = "vscode-mock"; |
| 5 | + |
| 6 | +const clearMockStorage = async (page: Page) => { |
| 7 | + await page.evaluate((prefix: string) => { |
| 8 | + Object.keys(localStorage) |
| 9 | + .filter((k) => k.startsWith(prefix)) |
| 10 | + .forEach((k) => localStorage.removeItem(k)); |
| 11 | + }, STORAGE_KEY_PREFIX); |
| 12 | +}; |
| 13 | + |
| 14 | +test.describe("Configuration Persistence Across Page Refresh", () => { |
| 15 | + test.beforeEach(async ({ page }) => { |
| 16 | + await page.goto("/"); |
| 17 | + await clearMockStorage(page); |
| 18 | + await page.reload(); |
| 19 | + await page.waitForLoadState("networkidle"); |
| 20 | + }); |
| 21 | + |
| 22 | + test("should persist newly added command after page refresh", async ({ page }) => { |
| 23 | + // Given: Add a new command |
| 24 | + await page.getByRole("button", { name: /add/i }).first().click(); |
| 25 | + await fillCommandForm(page, { |
| 26 | + name: "Refresh Test Command", |
| 27 | + command: "echo refresh-test", |
| 28 | + shortcut: "r", |
| 29 | + }); |
| 30 | + await saveCommandDialog(page); |
| 31 | + |
| 32 | + // When: Apply changes and refresh |
| 33 | + await page.getByRole("button", { name: "Apply configuration changes" }).click(); |
| 34 | + await verifySuccessToast(page, "Configuration saved successfully"); |
| 35 | + await page.reload(); |
| 36 | + await page.waitForLoadState("networkidle"); |
| 37 | + |
| 38 | + // Then: Command should still be visible |
| 39 | + const commandCard = page.locator('[data-testid="command-card"]', { |
| 40 | + hasText: "Refresh Test Command", |
| 41 | + }); |
| 42 | + await expect(commandCard).toBeVisible(); |
| 43 | + await expect(commandCard.locator("code")).toContainText("echo refresh-test"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("should persist edited command after page refresh", async ({ page }) => { |
| 47 | + // Given: Add and save a command |
| 48 | + await page.getByRole("button", { name: /add/i }).first().click(); |
| 49 | + await fillCommandForm(page, { |
| 50 | + name: "Edit Persist Test", |
| 51 | + command: "echo original", |
| 52 | + }); |
| 53 | + await saveCommandDialog(page); |
| 54 | + await page.getByRole("button", { name: "Apply configuration changes" }).click(); |
| 55 | + await verifySuccessToast(page, "Configuration saved successfully"); |
| 56 | + |
| 57 | + // When: Edit the command |
| 58 | + const commandCard = page.locator('[data-testid="command-card"]', { |
| 59 | + hasText: "Edit Persist Test", |
| 60 | + }); |
| 61 | + await commandCard.getByRole("button", { name: /edit command/i }).click(); |
| 62 | + |
| 63 | + const commandInput = page.getByRole("textbox", { exact: true, name: "Command" }); |
| 64 | + await commandInput.clear(); |
| 65 | + await commandInput.fill("echo modified"); |
| 66 | + await saveCommandDialog(page); |
| 67 | + |
| 68 | + // Apply and refresh |
| 69 | + await page.getByRole("button", { name: "Apply configuration changes" }).click(); |
| 70 | + await verifySuccessToast(page, "Configuration saved successfully"); |
| 71 | + await page.reload(); |
| 72 | + await page.waitForLoadState("networkidle"); |
| 73 | + |
| 74 | + // Then: Modified command should be visible |
| 75 | + const modifiedCard = page.locator('[data-testid="command-card"]', { |
| 76 | + hasText: "Edit Persist Test", |
| 77 | + }); |
| 78 | + await expect(modifiedCard).toBeVisible(); |
| 79 | + await expect(modifiedCard.locator("code")).toContainText("echo modified"); |
| 80 | + }); |
| 81 | + |
| 82 | + test("should persist deleted command state after page refresh", async ({ page }) => { |
| 83 | + // Given: Add two commands |
| 84 | + await page.getByRole("button", { name: /add/i }).first().click(); |
| 85 | + await fillCommandForm(page, { name: "Keep This", command: "echo keep" }); |
| 86 | + await saveCommandDialog(page); |
| 87 | + |
| 88 | + await page.getByRole("button", { name: /add/i }).first().click(); |
| 89 | + await fillCommandForm(page, { name: "Delete This", command: "echo delete" }); |
| 90 | + await saveCommandDialog(page); |
| 91 | + |
| 92 | + await page.getByRole("button", { name: "Apply configuration changes" }).click(); |
| 93 | + await verifySuccessToast(page, "Configuration saved successfully"); |
| 94 | + |
| 95 | + // When: Delete one command |
| 96 | + const deleteCard = page.locator('[data-testid="command-card"]', { |
| 97 | + hasText: "Delete This", |
| 98 | + }); |
| 99 | + await deleteCard.getByRole("button", { name: /delete command/i }).click(); |
| 100 | + await page.getByRole("button", { name: "Delete" }).click(); |
| 101 | + |
| 102 | + await page.getByRole("button", { name: "Apply configuration changes" }).click(); |
| 103 | + await verifySuccessToast(page, "Configuration saved successfully"); |
| 104 | + await page.reload(); |
| 105 | + await page.waitForLoadState("networkidle"); |
| 106 | + |
| 107 | + // Then: Only the kept command should be visible |
| 108 | + const keptCard = page.locator('[data-testid="command-card"]', { |
| 109 | + hasText: "Keep This", |
| 110 | + }); |
| 111 | + await expect(keptCard).toBeVisible(); |
| 112 | + |
| 113 | + const deletedCard = page.locator('[data-testid="command-card"]', { |
| 114 | + hasText: "Delete This", |
| 115 | + }); |
| 116 | + await expect(deletedCard).not.toBeVisible(); |
| 117 | + }); |
| 118 | + |
| 119 | + test("should persist configuration scope selection after page refresh", async ({ page }) => { |
| 120 | + // Given: Switch to global scope |
| 121 | + await page.getByRole("radio", { name: /global/i }).click(); |
| 122 | + await page.waitForTimeout(200); |
| 123 | + |
| 124 | + // When: Refresh the page |
| 125 | + await page.reload(); |
| 126 | + await page.waitForLoadState("networkidle"); |
| 127 | + |
| 128 | + // Then: Global scope should still be selected |
| 129 | + const globalRadio = page.getByRole("radio", { name: /global/i }); |
| 130 | + await expect(globalRadio).toBeChecked(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments