mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(browser): confirm before clearing browsing data (#65124)
This commit is contained in:
committed by
GitHub
Unverified
parent
8b36735d51
commit
bfee96a859
@@ -6,20 +6,37 @@ const source = (path: string) => sourceURL(new URL(path, import.meta.url))
|
||||
const modules = {
|
||||
fixture: source("../../gui-plugins/src/browser/settings.fixture.tsx"),
|
||||
ui: source("../src/runtime/plugin/ui.tsx"),
|
||||
// The app's own confirmation dialog, which `Dialogs.confirm` shows, and the language it reads.
|
||||
dialogs: source("../src/runtime/plugin/dialogs.tsx"),
|
||||
language: source("../src/runtime/i18n/language.tsx"),
|
||||
}
|
||||
|
||||
// Any story loads the app styles; the fixture mounts the real Settings > Browser page beside it. `target` is the entry
|
||||
// a settings search or the command palette is revealing.
|
||||
async function open(
|
||||
page: Page,
|
||||
options?: { target?: string; extensions?: boolean; installed?: boolean; historyKept?: boolean; file?: { imported: number; failed: number } | "unreadable" },
|
||||
options?: {
|
||||
target?: string
|
||||
extensions?: boolean
|
||||
installed?: boolean
|
||||
historyKept?: boolean
|
||||
clearFails?: boolean
|
||||
file?: { imported: number; failed: number } | "unreadable"
|
||||
},
|
||||
) {
|
||||
await page.evaluate(
|
||||
async (input) => {
|
||||
const [{ mountBrowserSettings }, { ui }] = await Promise.all([import(input.modules.fixture), import(input.modules.ui)])
|
||||
const [{ mountBrowserSettings }, { ui }, { PluginConfirmDialog }, { LanguageProvider }] = await Promise.all([
|
||||
import(input.modules.fixture),
|
||||
import(input.modules.ui),
|
||||
import(input.modules.dialogs),
|
||||
import(input.modules.language),
|
||||
])
|
||||
|
||||
mountBrowserSettings({
|
||||
ui,
|
||||
host: { Confirm: PluginConfirmDialog, LanguageProvider },
|
||||
clearFails: input.options?.clearFails,
|
||||
target: input.options?.target,
|
||||
extensions: input.options?.extensions,
|
||||
installed: input.options?.installed,
|
||||
@@ -36,6 +53,13 @@ async function open(
|
||||
root,
|
||||
advanced: root.getByRole("button", { name: /^Advanced/ }),
|
||||
stored: root.getByTestId("browser-settings-stored"),
|
||||
data: root.getByRole("region", { name: "Browser data", exact: true }),
|
||||
// The red button, and the question it asks before anything is cleared.
|
||||
clear: root.getByRole("region", { name: "Browser data", exact: true }).getByRole("button", { name: "Clear browsing data", exact: true }),
|
||||
confirm: page.getByRole("dialog", { name: "Clear browsing data?", exact: true }),
|
||||
// How often main was asked to clear. The dialog is modal, so this is hidden from the accessibility tree meanwhile.
|
||||
clears: root.getByTestId("browser-settings-clears"),
|
||||
visits: root.getByTestId("browser-settings-visits"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,18 +123,72 @@ story("each switch is named by its row's title, and each row says what it does i
|
||||
await expect(sites.locator("p")).toHaveText("Agent and remote server tabs ask again. Clearing browsing data keeps saved permissions.")
|
||||
})
|
||||
|
||||
story("clearing browsing data that left the history behind says so in its row, and still forgets the window's last pages", async ({ page }) => {
|
||||
const view = await open(page, { target: "settings-browser-cookies", historyKept: true })
|
||||
const data = view.root.getByRole("region", { name: "Browser data", exact: true })
|
||||
const clear = data.getByRole("button", { name: "Clear browsing data", exact: true })
|
||||
story("the red button asks first: Cancel, Escape, Enter on Cancel and a click outside clear nothing, and the confirm clears once", async ({ page }) => {
|
||||
const view = await open(page, { target: "settings-browser-cookies" })
|
||||
const cancel = view.confirm.getByRole("button", { name: "Cancel", exact: true })
|
||||
const confirm = view.confirm.getByRole("button", { name: "Clear data", exact: true })
|
||||
|
||||
await expect(view.root.getByTestId("browser-settings-visits")).toHaveText("1")
|
||||
await expect(data.getByText("0 cookies. Sign-ins are shared across sessions.", { exact: true })).toBeVisible()
|
||||
await clear.click()
|
||||
await expect(view.data.getByText("0 cookies. Sign-ins are shared across sessions.", { exact: true })).toBeVisible()
|
||||
await view.clear.click()
|
||||
// One title, one line, two buttons: no close button, as the app's other confirmations.
|
||||
await expect(view.confirm.getByText("This signs you out of all sites and deletes history in OpenCode, leaving your other browsers untouched.", { exact: true })).toBeVisible()
|
||||
await expect(view.confirm.getByRole("button")).toHaveText(["Cancel", "Clear data"])
|
||||
// As the app's destructive confirmations do, it opens on Cancel, so Enter does not clear by accident.
|
||||
await expect(cancel).toBeFocused()
|
||||
await page.keyboard.press("Tab")
|
||||
await expect(confirm).toBeFocused()
|
||||
await page.keyboard.press("Shift+Tab")
|
||||
await page.keyboard.press("Enter")
|
||||
await expect(view.confirm).toHaveCount(0)
|
||||
await expect(view.clears).toHaveText("0")
|
||||
|
||||
for (const dismiss of [
|
||||
() => cancel.click(),
|
||||
() => page.keyboard.press("Escape"),
|
||||
// Outside the dialog: the dimmed page behind it.
|
||||
() => page.mouse.click(8, 8),
|
||||
]) {
|
||||
await view.clear.click()
|
||||
await expect(cancel).toBeFocused()
|
||||
await dismiss()
|
||||
await expect(view.confirm).toHaveCount(0)
|
||||
await expect(view.clears).toHaveText("0")
|
||||
}
|
||||
|
||||
// Nothing ran: the row still shows the count, the window's last pages are there, and the button is ready again.
|
||||
await expect(view.data.getByText("0 cookies. Sign-ins are shared across sessions.", { exact: true })).toBeVisible()
|
||||
await expect(view.visits).toHaveText("1")
|
||||
await expect(view.clear).toBeEnabled()
|
||||
|
||||
// Confirmed, with a second press on its way: main is asked once, and the row says how it ended.
|
||||
await view.clear.click()
|
||||
await confirm.dblclick()
|
||||
await expect(view.confirm).toHaveCount(0)
|
||||
await expect(view.data.getByText("Cleared browsing data.", { exact: true })).toBeVisible()
|
||||
await expect(view.clears).toHaveText("1")
|
||||
await expect(view.visits).toHaveText("0")
|
||||
})
|
||||
|
||||
story("a confirmed clear that left the history behind says so in its row", async ({ page }) => {
|
||||
const kept = await open(page, { target: "settings-browser-cookies", historyKept: true })
|
||||
|
||||
await kept.clear.click()
|
||||
await kept.confirm.getByRole("button", { name: "Clear data", exact: true }).click()
|
||||
// The sign-ins and site data went, so the row does not call it failed; it says what stayed.
|
||||
await expect(data.getByText("Cleared browsing data. The history could not be cleared.", { exact: true })).toBeVisible()
|
||||
await expect(view.root.getByTestId("browser-settings-visits")).toHaveText("0")
|
||||
await expect(clear).toBeEnabled()
|
||||
await expect(kept.data.getByText("Cleared sign-ins and site data, but history could not be cleared.", { exact: true })).toBeVisible()
|
||||
await expect(kept.visits).toHaveText("0")
|
||||
await expect(kept.clear).toBeEnabled()
|
||||
})
|
||||
|
||||
story("a confirmed clear that main rejects says that it could not clear, and can be tried again", async ({ page }) => {
|
||||
const view = await open(page, { target: "settings-browser-cookies", clearFails: true })
|
||||
|
||||
await view.clear.click()
|
||||
await view.confirm.getByRole("button", { name: "Clear data", exact: true }).click()
|
||||
await expect(view.data.getByText("Couldn't clear browsing data. Try again.", { exact: true })).toBeVisible()
|
||||
await expect(view.clears).toHaveText("1")
|
||||
await expect(view.visits).toHaveText("1")
|
||||
await expect(view.clear).toBeEnabled()
|
||||
})
|
||||
|
||||
// A cookie file is cookies, so its row counts them: the one place an import says "cookies".
|
||||
|
||||
@@ -239,7 +239,10 @@ export default {
|
||||
"settings.cookies.count.other": "{{count}} cookies. Sign-ins are shared across sessions.",
|
||||
"settings.cookies.clear": "Clear browsing data",
|
||||
"settings.cleared": "Cleared browsing data.",
|
||||
"settings.cleared.history": "Cleared browsing data. The history could not be cleared.",
|
||||
"settings.cleared.history": "Cleared sign-ins and site data, but history could not be cleared.",
|
||||
"settings.clear.confirm.title": "Clear browsing data?",
|
||||
"settings.clear.confirm.description": "This signs you out of all sites and deletes history in OpenCode, leaving your other browsers untouched.",
|
||||
"settings.clear.confirm.action": "Clear data",
|
||||
"menu.back": "Back",
|
||||
"menu.forward": "Forward",
|
||||
"menu.reload": "Reload",
|
||||
|
||||
@@ -42,7 +42,7 @@ function replay(events: readonly JobsEvent[]) {
|
||||
test("runs one job at a time and reports the outcome with the job that ran", () => {
|
||||
const result = replay([
|
||||
{ type: "started", job: edge },
|
||||
// A second click while the import runs starts nothing.
|
||||
// A second click while the import runs starts nothing, and asks nothing.
|
||||
{ type: "started", job: { type: "clear" } },
|
||||
{ type: "finished", attempt: 1, outcome: imported },
|
||||
])
|
||||
@@ -65,7 +65,7 @@ test("a timed-out job ends in an error, and its late reply changes nothing", ()
|
||||
{ type: "started", job: store },
|
||||
{ type: "timed-out", attempt: 1 },
|
||||
{ type: "finished", attempt: 1, outcome: imported },
|
||||
{ type: "started", job: { type: "clear" } },
|
||||
{ type: "started", job: edge },
|
||||
{ type: "failed", attempt: 1 },
|
||||
])
|
||||
|
||||
@@ -74,7 +74,7 @@ test("a timed-out job ends in an error, and its late reply changes nothing", ()
|
||||
{ type: "abort", attempt: 1 },
|
||||
{ type: "notify", finished: { job: store, outcome: { type: "error" } } },
|
||||
])
|
||||
expect(runningJob(result.state)).toEqual({ type: "clear" })
|
||||
expect(runningJob(result.state)).toEqual(edge)
|
||||
})
|
||||
|
||||
test("dismissing clears the last outcome only while nothing runs", () => {
|
||||
@@ -93,15 +93,90 @@ test("dismissing clears the last outcome only while nothing runs", () => {
|
||||
|
||||
test("leaving the surface aborts the running job, and nothing runs after", () => {
|
||||
const result = replay([
|
||||
{ type: "started", job: { type: "clear" } },
|
||||
{ type: "started", job: edge },
|
||||
{ type: "disposed" },
|
||||
{ type: "finished", attempt: 1, outcome: { type: "cleared", history: true } },
|
||||
{ type: "started", job: { type: "clear" } },
|
||||
{ type: "finished", attempt: 1, outcome: imported },
|
||||
{ type: "started", job: edge },
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "run", attempt: 1, job: { type: "clear" }, timeoutMs: 60_000 },
|
||||
{ type: "run", attempt: 1, job: edge, timeoutMs: 300_000 },
|
||||
{ type: "abort", attempt: 1 },
|
||||
])
|
||||
expect(result.state).toEqual({ status: "disposed" })
|
||||
})
|
||||
|
||||
// Clearing the browsing data cannot be undone, so the person is asked first. The pane's driver answers "no" for
|
||||
// Cancel, Escape, a click outside and a question that closed with its surface, so each of those is `confirmed: false`.
|
||||
const clear: Job = { type: "clear" }
|
||||
|
||||
const cleared = { type: "cleared" as const, history: true }
|
||||
|
||||
const ran: JobsEffect = { type: "run", attempt: 1, job: clear, timeoutMs: 60_000 }
|
||||
|
||||
const asked: JobsEffect = { type: "confirm", attempt: 1, job: clear }
|
||||
|
||||
test("clearing asks first: nothing runs until the person confirms, and a no leaves the surface as it was", () => {
|
||||
const before = replay([{ type: "started", job: edge }, { type: "finished", attempt: 1, outcome: imported }])
|
||||
const open = transitionJobs(before.state, { type: "started", job: clear })
|
||||
|
||||
// Only the question: no call to main. The last result stays on the surface behind it.
|
||||
expect(open.effects).toEqual([{ type: "confirm", attempt: 2, job: clear }])
|
||||
expect(runningJob(open.state)).toBeUndefined()
|
||||
expect(lastFinished(open.state)).toEqual({ job: edge, outcome: imported })
|
||||
|
||||
// Cancel, Escape or a click outside: nothing ran, and the surface is as before the question.
|
||||
const declined = transitionJobs(open.state, { type: "answered", attempt: 2, confirmed: false })
|
||||
expect(declined.effects).toEqual([])
|
||||
expect(declined.state).toEqual({ status: "idle", attempt: 2, last: { job: edge, outcome: imported } })
|
||||
|
||||
// Asked again and confirmed, it runs once, under the attempt its question claimed.
|
||||
const again = transitionJobs(declined.state, { type: "started", job: clear })
|
||||
const confirmed = transitionJobs(again.state, { type: "answered", attempt: 3, confirmed: true })
|
||||
expect(confirmed.effects).toEqual([{ type: "run", attempt: 3, job: clear, timeoutMs: 60_000 }])
|
||||
expect(runningJob(confirmed.state)).toEqual(clear)
|
||||
})
|
||||
|
||||
test("a confirmed clear runs once, however often the button or the answer is pressed", () => {
|
||||
const result = replay([
|
||||
{ type: "started", job: clear },
|
||||
// The red button again, and another job, while the question is open: neither starts anything.
|
||||
{ type: "started", job: clear },
|
||||
{ type: "started", job: edge },
|
||||
{ type: "answered", attempt: 1, confirmed: true },
|
||||
// A second press of the dialog's button, and a late no, change nothing.
|
||||
{ type: "answered", attempt: 1, confirmed: true },
|
||||
{ type: "answered", attempt: 1, confirmed: false },
|
||||
{ type: "finished", attempt: 1, outcome: cleared },
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([asked, ran, { type: "abort", attempt: 1 }, { type: "notify", finished: { job: clear, outcome: cleared } }])
|
||||
expect(lastFinished(result.state)).toEqual({ job: clear, outcome: cleared })
|
||||
})
|
||||
|
||||
test("an answer to an earlier question, or after the surface went, clears nothing", () => {
|
||||
const stale = replay([
|
||||
{ type: "started", job: clear },
|
||||
{ type: "answered", attempt: 1, confirmed: false },
|
||||
{ type: "started", job: clear },
|
||||
// The first question's yes arrives late: the open question is the second.
|
||||
{ type: "answered", attempt: 1, confirmed: true },
|
||||
])
|
||||
|
||||
expect(stale.effects).toEqual([asked, { type: "confirm", attempt: 2, job: clear }])
|
||||
expect(stale.state).toMatchObject({ status: "confirming", attempt: 2 })
|
||||
|
||||
// The surface goes while the question is open: no call was made, so there is none to abort, and a yes runs nothing.
|
||||
const gone = replay([{ type: "started", job: clear }, { type: "disposed" }, { type: "answered", attempt: 1, confirmed: true }])
|
||||
expect(gone.effects).toEqual([asked])
|
||||
expect(gone.state).toEqual({ status: "disposed" })
|
||||
})
|
||||
|
||||
test("a confirmed clear that fails or times out ends in an error the surface says", () => {
|
||||
for (const failure of [{ type: "failed" as const, attempt: 1 }, { type: "timed-out" as const, attempt: 1 }]) {
|
||||
const result = replay([{ type: "started", job: clear }, { type: "answered", attempt: 1, confirmed: true }, failure])
|
||||
|
||||
expect(result.effects).toEqual([asked, ran, { type: "abort", attempt: 1 }, { type: "notify", finished: { job: clear, outcome: { type: "error" } } }])
|
||||
expect(lastFinished(result.state)).toEqual({ job: clear, outcome: { type: "error" } })
|
||||
}
|
||||
})
|
||||
|
||||
@@ -46,15 +46,20 @@ export type Finished = { readonly job: Job; readonly outcome: Outcome }
|
||||
/**
|
||||
* A surface runs one job at a time; main runs the jobs of every surface one after another.
|
||||
* - `idle`: nothing runs; `last` is the previous job and its outcome, until the next one starts.
|
||||
* - `confirming`: the person is asked whether `job` should run, as `attempt`; nothing has run yet, and `last` comes
|
||||
* back if they say no. An answer to any other attempt is stale.
|
||||
* - `running`: `job` runs as `attempt`; a result of any other attempt is stale.
|
||||
*/
|
||||
export type JobsState =
|
||||
| { readonly status: "idle"; readonly attempt: number; readonly last: Finished | undefined }
|
||||
| { readonly status: "confirming"; readonly attempt: number; readonly job: Job; readonly last: Finished | undefined }
|
||||
| { readonly status: "running"; readonly attempt: number; readonly job: Job }
|
||||
| { readonly status: "disposed" }
|
||||
|
||||
export type JobsEvent =
|
||||
| { readonly type: "started"; readonly job: Job }
|
||||
/** The person answered the question of `attempt`. Closing the question any other way is a no. */
|
||||
| { readonly type: "answered"; readonly attempt: number; readonly confirmed: boolean }
|
||||
| { readonly type: "finished"; readonly attempt: number; readonly outcome: Outcome }
|
||||
| { readonly type: "failed"; readonly attempt: number }
|
||||
| { readonly type: "timed-out"; readonly attempt: number }
|
||||
@@ -63,6 +68,8 @@ export type JobsEvent =
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type JobsEffect =
|
||||
/** Asks the person whether `job` should run; the answer names `attempt`. */
|
||||
| { readonly type: "confirm"; readonly attempt: number; readonly job: Job }
|
||||
| { readonly type: "run"; readonly attempt: number; readonly job: Job; readonly timeoutMs: number }
|
||||
/** Ends the attempt's timer and drops its call's reply. */
|
||||
| { readonly type: "abort"; readonly attempt: number }
|
||||
@@ -104,13 +111,24 @@ export function transitionJobs(state: JobsState, event: JobsEvent): JobsTransiti
|
||||
}
|
||||
|
||||
if (event.type === "started") {
|
||||
if (state.status === "running") return { state, effects: [] }
|
||||
// One job at a time: a second press while one runs, or while its question is open, starts nothing.
|
||||
if (state.status !== "idle") return { state, effects: [] }
|
||||
const attempt = state.attempt + 1
|
||||
|
||||
return {
|
||||
state: { status: "running", attempt, job: event.job },
|
||||
effects: [{ type: "run", attempt, job: event.job, timeoutMs: JOB_TIMEOUT_MS[event.job.type] }],
|
||||
}
|
||||
// What a job deletes cannot be brought back, so it asks first, wherever it was started from.
|
||||
if (deletes(event.job))
|
||||
return { state: { status: "confirming", attempt, job: event.job, last: state.last }, effects: [{ type: "confirm", attempt, job: event.job }] }
|
||||
|
||||
return run(attempt, event.job)
|
||||
}
|
||||
|
||||
if (event.type === "answered") {
|
||||
if (state.status !== "confirming" || event.attempt !== state.attempt) return { state, effects: [] }
|
||||
|
||||
if (event.confirmed) return run(state.attempt, state.job)
|
||||
|
||||
// Nothing ran: the surface is as it was before the question.
|
||||
return { state: { status: "idle", attempt: state.attempt, last: state.last }, effects: [] }
|
||||
}
|
||||
|
||||
if (state.status !== "running" || event.attempt !== state.attempt) return { state, effects: [] }
|
||||
@@ -132,8 +150,18 @@ export function runningJob(state: JobsState) {
|
||||
return state.status === "running" ? state.job : undefined
|
||||
}
|
||||
|
||||
/** The last job and its outcome, which stay on the surface while the person is asked about the next one. */
|
||||
export function lastFinished(state: JobsState) {
|
||||
return state.status === "idle" ? state.last : undefined
|
||||
return state.status === "idle" || state.status === "confirming" ? state.last : undefined
|
||||
}
|
||||
|
||||
/** Whether a job deletes what cannot be brought back, so the person is asked before it runs. */
|
||||
function deletes(job: Job) {
|
||||
return job.type === "clear"
|
||||
}
|
||||
|
||||
function run(attempt: number, job: Job): JobsTransition {
|
||||
return { state: { status: "running", attempt, job }, effects: [{ type: "run", attempt, job, timeoutMs: JOB_TIMEOUT_MS[job.type] }] }
|
||||
}
|
||||
|
||||
/** Whether an import wrote cookies, so the user is signed in where the other browser was. */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createSignal, onCleanup, untrack } from "solid-js"
|
||||
import { createSignal, getOwner, onCleanup, runWithOwner, untrack } from "solid-js"
|
||||
import { createKeyed, createLatest, usePlugin, type IpcClient } from "@opencode/plugin/gui"
|
||||
import type { Choice } from "./choices"
|
||||
import type definition from "./index"
|
||||
@@ -69,6 +69,18 @@ export function createProfileJobs(
|
||||
return current.status === "active" ? `${current.generation}:${version()}` : undefined
|
||||
}
|
||||
|
||||
// A question belongs to the surface that asked, so it goes with it.
|
||||
const owner = getOwner()
|
||||
|
||||
// What the person is asked before the one job that deletes: clearing the browsing data.
|
||||
const question = () => ({
|
||||
title: ctx.t("settings.clear.confirm.title"),
|
||||
description: ctx.t("settings.clear.confirm.description"),
|
||||
confirm: ctx.t("settings.clear.confirm.action"),
|
||||
cancel: ctx.t("common.cancel"),
|
||||
danger: true,
|
||||
})
|
||||
|
||||
const dispatch = (event: JobsEvent) => {
|
||||
const next = transitionJobs(untrack(state), event)
|
||||
setState(next.state)
|
||||
@@ -76,6 +88,19 @@ export function createProfileJobs(
|
||||
}
|
||||
|
||||
const run = (effect: JobsEffect) => {
|
||||
if (effect.type === "confirm") {
|
||||
const attempt = effect.attempt
|
||||
|
||||
// The app's own confirmation: it starts on Cancel, and Escape or a click outside answers no. It closes with
|
||||
// the surface that asked, which is a no too, as is a question that could not be asked.
|
||||
const asked = runWithOwner(owner, () => ctx.dialogs.confirm(question())) ?? Promise.resolve(false)
|
||||
|
||||
return void asked.then(
|
||||
(confirmed) => dispatch({ type: "answered", attempt, confirmed }),
|
||||
() => dispatch({ type: "answered", attempt, confirmed: false }),
|
||||
)
|
||||
}
|
||||
|
||||
if (effect.type === "notify") {
|
||||
setVersion((value) => value + 1)
|
||||
input.finished?.(effect.finished)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { DialogProvider, useDialog } from "@opencode/ui/context/dialog"
|
||||
import type { JSX } from "solid-js"
|
||||
import type { Component, JSX } from "solid-js"
|
||||
import { createStore, produce } from "solid-js/store"
|
||||
import { render } from "solid-js/web"
|
||||
import { liveAccessor, PluginContext } from "@opencode/plugin/gui/host"
|
||||
import type { Context, SetupContext, Ui } from "@opencode/plugin/gui"
|
||||
import type { ConfirmOptions, Context, SetupContext, Ui } from "@opencode/plugin/gui"
|
||||
import browserEn from "./i18n/en"
|
||||
import type definition from "./index"
|
||||
import type { ExtensionAccess, ExtensionCompatibility, ImportResult, InstallResult, ProfileSource } from "./profile/ipc"
|
||||
@@ -23,6 +23,12 @@ const badge = (bg: string, fg: string, glyph: string) =>
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20"><rect width="20" height="20" rx="4" fill="${bg}"/>${glyph.replaceAll("FG", fg)}</svg>`,
|
||||
)}`
|
||||
|
||||
/** What the fixture takes from the app, which this package cannot import. */
|
||||
type SettingsHost = {
|
||||
readonly Confirm: Component<{ options: ConfirmOptions; done: (confirmed: boolean) => void }>
|
||||
readonly LanguageProvider: Component<{ locale: string; children: JSX.Element }>
|
||||
}
|
||||
|
||||
const sampleSources: readonly ProfileSource[] = [
|
||||
{ id: "helium", name: "Helium", engine: "chromium", profiles: [{ id: "Default", name: "Default" }] },
|
||||
{ id: "chrome", name: "Chrome", engine: "chromium", profiles: [{ id: "Default", name: "Default" }] },
|
||||
@@ -78,13 +84,17 @@ const sampleExtensionEntries: readonly (readonly [string, readonly SampleExtensi
|
||||
// which stays set for the whole visit, as the host leaves it. With `extensions`, Chromium browsers and their extensions
|
||||
// are populated so the real Add an extension dialog can be driven end to end. `historyKept` is a sign-out that left
|
||||
// main's history behind: its file could not be removed. `file` is what importing the cookie file the person picks
|
||||
// brings: how many cookies came and how many could not be imported, or that the file could not be read.
|
||||
// brings: how many cookies came and how many could not be imported, or that the file could not be read. `clearFails`
|
||||
// is main rejecting the clear. `host` is the app's own confirmation dialog, which `Dialogs.confirm` shows, with the
|
||||
// language it reads its default labels from.
|
||||
export function mountBrowserSettings(input: {
|
||||
ui: Ui
|
||||
host: SettingsHost
|
||||
target?: string
|
||||
extensions?: boolean
|
||||
installed?: boolean
|
||||
historyKept?: boolean
|
||||
clearFails?: boolean
|
||||
file?: { imported: number; failed: number } | "unreadable"
|
||||
}) {
|
||||
const host = document.createElement("main")
|
||||
@@ -105,6 +115,9 @@ export function mountBrowserSettings(input: {
|
||||
welcomed: true,
|
||||
})
|
||||
|
||||
// How often main was asked to clear the browsing data.
|
||||
const [calls, setCalls] = createStore({ clear: 0 })
|
||||
|
||||
// The window's list of the last pages, which a sign-out empties.
|
||||
const [history, setHistory] = createStore<{ visits: readonly { url: string; title: string }[] }>({
|
||||
visits: [{ url: "https://alpha.example/", title: "Alpha" }],
|
||||
@@ -134,7 +147,13 @@ export function mountBrowserSettings(input: {
|
||||
on: () => () => undefined,
|
||||
sources: async () => (input.extensions ? [...sampleSources] : []),
|
||||
summary: async () => ({ cookies: 0, persistent: true }),
|
||||
clear: async () => ({ history: !input.historyKept }),
|
||||
clear: async () => {
|
||||
setCalls("clear", (count) => count + 1)
|
||||
|
||||
if (input.clearFails) throw new Error("the profile could not be cleared")
|
||||
|
||||
return { history: !input.historyKept }
|
||||
},
|
||||
importFile: async (): Promise<ImportResult> => {
|
||||
if (input.file === "unreadable") return { type: "failed", reason: "read" }
|
||||
|
||||
@@ -227,6 +246,24 @@ export function mountBrowserSettings(input: {
|
||||
|
||||
return handle
|
||||
},
|
||||
// As the host does: the content's answer, or no once the dialog ends without one; the first wins.
|
||||
confirm: (options: ConfirmOptions) =>
|
||||
new Promise<boolean>((resolve) =>
|
||||
dialog.show(
|
||||
() => (
|
||||
<input.host.LanguageProvider locale="en">
|
||||
<input.host.Confirm
|
||||
options={options}
|
||||
done={(confirmed) => {
|
||||
resolve(confirmed)
|
||||
dialog.close()
|
||||
}}
|
||||
/>
|
||||
</input.host.LanguageProvider>
|
||||
),
|
||||
() => resolve(false),
|
||||
),
|
||||
),
|
||||
},
|
||||
stores: {
|
||||
preferences: {
|
||||
@@ -248,6 +285,7 @@ export function mountBrowserSettings(input: {
|
||||
<BrowserSettings target={input.target} />
|
||||
<output data-testid="browser-settings-stored">{JSON.stringify(preferences)}</output>
|
||||
<output data-testid="browser-settings-visits">{history.visits.length}</output>
|
||||
<output data-testid="browser-settings-clears">{calls.clear}</output>
|
||||
</PluginContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user