askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
history.mjs · 11.0 KB · 136 lines
| 1 | // Browsing history on the askrobots account, end to end against the stand-in on the demo server (see /mcp there): |
| 2 | // nothing before signing in; visits with clean addresses and their text after; glances, password pages and paused time |
| 3 | // left out; forget and never-record; the status bar; and the assistant's history search, which asks first. |
| 4 | export default async function (ctx) { |
| 5 | const { call: T, check, base, sleep, chrome } = ctx; |
| 6 | const stored = async () => (await fetch(base + "/o/_history")).json(); |
| 7 | async function waitFor(fn, what, tries = 120) { |
| 8 | for (let i = 0; i < tries; i++) { const v = await fn(); if (v) return v; await sleep(100); } |
| 9 | throw new Error(`timed out waiting for ${what}`); |
| 10 | } |
| 11 | const ask = (id) => T("mcp.state").then((s) => s.find((x) => x.id === id)); |
| 12 | const c = await chrome(); |
| 13 | const pill = () => c.ev(`(() => { const b = document.getElementById('histBtn'); return b.style.display === 'none' ? '' : b.textContent; })()`); |
| 14 | // Visit a page for `secs` seconds, then move on (to a blank page, which ends the visit). |
| 15 | async function visit(url, secs) { |
| 16 | await T("page.goto", { url }); |
| 17 | await sleep(secs * 1000); |
| 18 | } |
| 19 | |
| 20 | // ---- before signing in ---------------------------------------------------------------------------------------- |
| 21 | let st = await T("history.state"); |
| 22 | check(st.on === false && /not signed in/.test(st.why), "before signing in to askrobots, history is off and says why", JSON.stringify(st)); |
| 23 | await visit(base + "/free", 2.5); |
| 24 | await T("page.goto", { url: base + "/tiny" }); |
| 25 | await T("history.flush"); |
| 26 | check((await stored()).visits.length === 0 && (await T("history.state")).queued === 0, "and nothing is recorded or kept waiting"); |
| 27 | check((await pill()) === "", "the status bar shows no history button while signed out"); |
| 28 | |
| 29 | // ---- sign in ---------------------------------------------------------------------------------------------------- |
| 30 | const conn = await T("mcp.connect", { id: "askrobots", open: false }); |
| 31 | const back = (await fetch(conn.authorizeUrl, { redirect: "manual" })).headers.get("location"); |
| 32 | await fetch(back); |
| 33 | await waitFor(async () => (await ask("askrobots"))?.signedIn, "the sign-in"); |
| 34 | st = await waitFor(async () => { const s = await T("history.state"); return s.on ? s : null; }, "history to switch on"); |
| 35 | check(st.on && !st.paused, "signed in (with the history permission), history is on", JSON.stringify(st)); |
| 36 | check(new URL(conn.authorizeUrl).searchParams.get("scope").split(" ").includes("history"), "the sign-in asks for the history permission", new URL(conn.authorizeUrl).searchParams.get("scope")); |
| 37 | await waitFor(async () => (await pill()) === "History", "the status bar's History button"); |
| 38 | check(true, "the status bar shows History"); |
| 39 | |
| 40 | // ---- visits ----------------------------------------------------------------------------------------------------- |
| 41 | await visit(base + "/headers?utm_source=newsletter&x=1#top", 3); // a visit: kept, with the address cleaned |
| 42 | await visit(base + "/forms", 3); // a password field shows: never recorded |
| 43 | await visit(base + "/free", 0.5); // a glance: not a visit |
| 44 | await visit(base + "/readable", 3); |
| 45 | await visit(base + "/links", 3); |
| 46 | await T("page.goto", { url: base + "/tiny" }); |
| 47 | const after = await T("history.flush"); |
| 48 | const v = (await stored()).visits; |
| 49 | const urls = v.map((x) => x.url); |
| 50 | check(urls.includes(base + "/headers") && urls.includes(base + "/readable"), "pages you stay on are recorded on the account, with tracking parameters and fragments removed", JSON.stringify(urls)); |
| 51 | check(!urls.some((u) => u.includes("/forms")), "a page with a password field is never recorded"); |
| 52 | check(!urls.some((u) => u.endsWith("/free")), "a glance of under two seconds is not a visit"); |
| 53 | const h = v.find((x) => x.url === base + "/headers"); |
| 54 | check(h && h.seconds_on_page >= 2 && /^\d{4}-\d\d-\d\dT/.test(h.visited_at) && h.text && h.text.length > 20, "a visit has its start time, seconds on the page and the page's visible text", JSON.stringify({ ...h, text: h?.text?.slice(0, 60) })); |
| 55 | check(h && !h.title, "a page without a title is kept without one (not its address, which would bring the parameters back)", JSON.stringify(h?.title)); |
| 56 | check(v.find((x) => x.url === base + "/links")?.title === "Links", "a page's title is kept", JSON.stringify(v.map((x) => x.title))); |
| 57 | check(after.queued === 0 && after.sent >= 2, "nothing is left waiting once sent", JSON.stringify(after)); |
| 58 | |
| 59 | // ---- pause ------------------------------------------------------------------------------------------------------ |
| 60 | await T("history.pause", { paused: true }); |
| 61 | check((await stored()).paused === true && (await pill()) === "History paused", "pausing is saved on the account and shown in the status bar"); |
| 62 | const n0 = (await stored()).visits.length; |
| 63 | await visit(base + "/headers?paused=1", 3); |
| 64 | await T("page.goto", { url: base + "/tiny" }); |
| 65 | await T("history.flush"); |
| 66 | check((await stored()).visits.length === n0, "while paused, nothing is recorded"); |
| 67 | await T("history.pause", { paused: false }); |
| 68 | check((await stored()).paused === false && (await pill()) === "History", "resuming is saved too"); |
| 69 | |
| 70 | // ---- the assistant searches history, and asks first ------------------------------------------------------------ |
| 71 | let since = (await T("chat.events", { since: 0 })).next; |
| 72 | const run = T("chat.send", { text: "history: where did I see the headers page?", mode: "auto" }); |
| 73 | const q1 = await waitFor(async () => (await T("chat.events", { since })).events.find((e) => e.kind === "confirm"), "the approval for the history search"); |
| 74 | check(/^Search your browsing history/.test(q1.summary) && /AI/.test(q1.summary) && q1.danger === false, "the assistant's history search asks first, even in Auto mode, saying the results go to its AI (not marked risky: it changes nothing)", q1.summary); |
| 75 | await T("chat.confirm", { id: q1.id, ok: true }); |
| 76 | const r1 = await run; |
| 77 | check(r1.reply.includes(base + "/headers"), "with a yes, it finds the page in history", r1.reply); |
| 78 | since = (await T("chat.events", { since: 0 })).next; |
| 79 | const run2 = T("chat.send", { text: "history again" }); |
| 80 | const q2 = await waitFor(async () => (await T("chat.events", { since })).events.find((e) => e.kind === "confirm"), "the second approval"); |
| 81 | await T("chat.confirm", { id: q2.id, ok: false }); |
| 82 | check((await run2).reply === "Result: declined", "with a no, nothing is searched"); |
| 83 | const tools = await T("mcp.tools"); |
| 84 | check(!JSON.stringify(tools).match(/add_history|forget_history|list_history|history_settings/), "the assistant is never given the tools that record, list or forget history", JSON.stringify(tools.map((t) => t.name ?? t))); |
| 85 | |
| 86 | // ---- the address bar suggests from history (a copy kept here, so typing sends nothing) --------------------------- |
| 87 | const typeUrl = (text) => c.ev(`(() => { const u = document.getElementById('url'); u.focus(); u.value = ${JSON.stringify(text)}; u.dispatchEvent(new InputEvent('input', { inputType: 'insertText' })); return true; })()`); |
| 88 | const omniRows = () => c.ev(`[...document.querySelectorAll('#omni.open .or')].map((r) => ({ kind: r.dataset.kind, title: r.querySelector('.tt').textContent, url: (r.querySelector('.u') || {}).textContent || '', icon: !!r.querySelector('.ic img.fav') }))`); |
| 89 | const closeOmni = () => c.ev(`(() => { const u = document.getElementById('url'); u.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); u.blur(); return true; })()`); |
| 90 | const copySt = await T("history.syncCopy"); |
| 91 | check(copySt && copySt.visits >= 2, "the browser keeps a copy of the history list for the address bar", JSON.stringify(copySt)); |
| 92 | const lookups = (await stored()).visits.length; |
| 93 | await typeUrl("links"); |
| 94 | const hr = await waitFor(async () => (await omniRows()).find((r) => r.kind === "history") || null, "a history row", 40).catch(() => null); |
| 95 | check(hr && hr.title === "Links" && hr.url === base + "/links", "typing a page's title suggests it from history", JSON.stringify(await omniRows())); |
| 96 | await closeOmni(); |
| 97 | await typeUrl("readable"); |
| 98 | const hr2 = await waitFor(async () => (await omniRows()).find((r) => r.kind === "history") || null, "a history row by address", 40).catch(() => null); |
| 99 | check(hr2 && hr2.url === base + "/readable", "and part of its address does too", JSON.stringify(await omniRows())); |
| 100 | await closeOmni(); |
| 101 | await fetch(base + "/o/_history/other?" + new URLSearchParams({ url: "https://moon.example/mars-institute", title: "Space institute aims for the Moon and Mars" })); |
| 102 | await T("history.syncCopy"); |
| 103 | await typeUrl("mars institute"); |
| 104 | const other = await waitFor(async () => (await omniRows()).find((r) => r.kind === "history") || null, "the other computer's page", 40).catch(() => null); |
| 105 | check(other && other.url === "https://moon.example/mars-institute", "a page visited on another computer is suggested too", JSON.stringify(await omniRows())); |
| 106 | check(hr && hr.icon && other && !other.icon, "a suggestion shows its site's icon when this computer has opened that site (typing fetches none)", JSON.stringify({ here: hr?.icon, other: other?.icon })); |
| 107 | await closeOmni(); |
| 108 | check((await stored()).visits.length === lookups + 1, "and typing sent nothing to the account (only the other computer's visit was added)"); |
| 109 | |
| 110 | // ---- forget and never-record ----------------------------------------------------------------------------------- |
| 111 | const f = await T("history.forget", { scope: "url", target: base + "/readable#x" }); |
| 112 | check(f.forgotten >= 1 && !(await stored()).visits.some((x) => x.url === base + "/readable"), "forgetting a page deletes it from the account", JSON.stringify(f)); |
| 113 | await typeUrl("readable"); |
| 114 | await sleep(600); |
| 115 | check(!(await omniRows()).some((r) => r.kind === "history"), "and the address bar stops suggesting it at once", JSON.stringify(await omniRows())); |
| 116 | await closeOmni(); |
| 117 | await fetch(base + "/o/_history/forget-other?" + new URLSearchParams({ url: "https://moon.example/mars-institute" })); |
| 118 | await T("history.syncCopy"); |
| 119 | await typeUrl("mars institute"); |
| 120 | await sleep(600); |
| 121 | check(!(await omniRows()).some((r) => r.kind === "history"), "a page forgotten on another computer goes from the suggestions here too", JSON.stringify(await omniRows())); |
| 122 | await closeOmni(); |
| 123 | await T("history.never", { host: "127.0.0.1" }); |
| 124 | check((await stored()).never.includes("127.0.0.1"), "never-record is saved on the account"); |
| 125 | const n1 = (await stored()).visits.length; |
| 126 | await visit(base + "/readable", 3); |
| 127 | await T("page.goto", { url: base + "/tiny" }); |
| 128 | await T("history.flush"); |
| 129 | check((await stored()).visits.length === n1, "and that site is no longer recorded"); |
| 130 | |
| 131 | // ---- sign out --------------------------------------------------------------------------------------------------- |
| 132 | await T("mcp.disconnect", { id: "askrobots" }); |
| 133 | st = await waitFor(async () => { const s = await T("history.state"); return !s.on ? s : null; }, "history to switch off"); |
| 134 | check(st.on === false && st.queued === 0, "signing out switches history off and keeps nothing waiting", JSON.stringify(st)); |
| 135 | check(st.copy && st.copy.visits === 0, "and deletes the address bar's copy of history", JSON.stringify(st.copy)); |
| 136 | } |