// Going into components and out again with the browser's back and forward // (the mouse's side buttons): every step is a browser history entry. Runs // the real composable in a stand-in browser with its own history. import * as vue from 'vue' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createProject } from '../app/lib/logic/ops' import { saveProject, setCurrentId, type ProjectMeta } from '../app/lib/logic/storage' import type { Project } from '../app/lib/logic/types' /** Top holds Mid, Mid holds Leaf. */ function nested(name = 'Nested'): Project { const p = createProject({ examples: false }) p.name = name p.components = [ { id: 'top', name: 'Top', symbol: 'box', nodes: [{ id: 'm', kind: 'chip', chip: 'mid', x: 0, y: 0 }], wires: [] }, { id: 'mid', name: 'Mid', symbol: 'box', nodes: [{ id: 'l', kind: 'chip', chip: 'leaf', x: 0, y: 0 }], wires: [] }, { id: 'leaf', name: 'Leaf', symbol: 'box', nodes: [{ id: 'g', kind: 'nand', x: 0, y: 0 }], wires: [] }, ] p.openId = 'top' return p } function stored(p: Project, id: string): ProjectMeta { const m: ProjectMeta = { id, name: p.name, updated: Date.now() } saveProject(m, p) return m } /** This browser: its storage and its tab's history, kept across reloads. */ let store: Map let entries: unknown[] let at: number /** A tab with the editor open, on the history entry `at`. */ async function openTab() { const events = new Map void)[]>() const on = (type: string, f: (e: unknown) => void) => events.set(type, [...(events.get(type) ?? []), f]) const fire = (type: string, e: unknown = {}) => events.get(type)?.forEach(f => f(e)) const history = { get state() { return entries[at] ?? null }, pushState(s: unknown) { entries.splice(at + 1) entries.push(structuredClone(s)) at++ }, replaceState(s: unknown) { entries[at] = structuredClone(s) }, back() { if (at === 0) return at-- fire('popstate', { state: history.state }) }, forward() { if (at === entries.length - 1) return at++ fire('popstate', { state: history.state }) }, } vi.stubGlobal('window', { addEventListener: on }) vi.stubGlobal('document', { addEventListener: on, visibilityState: 'visible' }) vi.stubGlobal('location', { pathname: '/', origin: 'http://localhost' }) vi.stubGlobal('history', history) for (const k of ['ref', 'computed', 'reactive', 'shallowRef', 'toRaw', 'watch'] as const) vi.stubGlobal(k, vue[k]) vi.resetModules() const { useEditor } = await import('../app/composables/useEditor') const ed = useEditor() ed.enterGame() return { ed, history, /** The open circuit and the crumbs to it. */ where: () => [...ed.nav.value, ed.def.value.id].join(' > '), /** A press of the mouse's back button: whether the page stopped the browser going back. */ mouseBack() { const e = { button: 3, defaultPrevented: false, preventDefault: () => (e.defaultPrevented = true) } fire('mouseup', e) if (!e.defaultPrevented) history.back() return e.defaultPrevented }, } } beforeEach(() => { store = new Map() entries = [null] at = 0 vi.stubGlobal('localStorage', { getItem: (k: string) => store.get(k) ?? null, setItem: (k: string, v: string) => void store.set(k, String(v)), removeItem: (k: string) => void store.delete(k), }) vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date'] }) stored(nested(), 'p1') setCurrentId('p1') }) afterEach(() => { vi.useRealTimers() vi.unstubAllGlobals() }) describe('the browser history walking the circuits', () => { it('goes back and forward through the components as they were entered, crumbs and all', async () => { const tab = await openTab() tab.ed.open('mid') tab.ed.open('leaf') expect(tab.where()).toBe('top > mid > leaf') tab.history.back() expect(tab.where()).toBe('top > mid') tab.history.back() expect(tab.where()).toBe('top') tab.history.forward() expect(tab.where()).toBe('top > mid') tab.history.forward() expect(tab.where()).toBe('top > mid > leaf') }) it('takes Main and the crumbs as steps too, so back returns to where you were', async () => { const tab = await openTab() tab.ed.open('mid') tab.ed.open('leaf') tab.ed.goMain() expect(tab.where()).toBe('top') tab.history.back() expect(tab.where()).toBe('top > mid > leaf') tab.ed.goTo(1) expect(tab.where()).toBe('top > mid') tab.history.back() expect(tab.where()).toBe('top > mid > leaf') // Clicking Main while there writes no step. tab.ed.goMain() const steps = entries.length tab.ed.goMain() expect(entries.length).toBe(steps) }) it('forgets the steps ahead when you go somewhere new, like a browser', async () => { const tab = await openTab() tab.ed.open('mid') tab.ed.open('leaf') tab.history.back() tab.history.back() tab.ed.open('leaf') tab.history.forward() expect(tab.where()).toBe('top > leaf') }) it('opens the circuit of the entry after a reload, and after going back to an entry from before one', async () => { let tab = await openTab() tab.ed.open('mid') tab.ed.open('leaf') tab = await openTab() expect(tab.where()).toBe('top > mid > leaf') // Back from here loads the page anew on the entry before (the saved project has Leaf open). at-- tab = await openTab() expect(tab.where()).toBe('top > mid') }) it('stops the mouse back button on the first entry instead of leaving the editor', async () => { const tab = await openTab() tab.ed.open('mid') expect(tab.mouseBack()).toBe(false) expect(tab.where()).toBe('top') expect(tab.mouseBack()).toBe(true) expect(tab.where()).toBe('top') }) it('takes another project as a step: back opens the one before at the circuit it had open', async () => { stored(nested('Other'), 'p2') const tab = await openTab() tab.ed.open('mid') tab.ed.switchProject('p2') expect(tab.ed.meta.value.id).toBe('p2') tab.ed.open('leaf') tab.history.back() expect([tab.ed.meta.value.id, tab.where()]).toEqual(['p2', 'top']) tab.history.back() expect([tab.ed.meta.value.id, tab.where()]).toEqual(['p1', 'top > mid']) tab.history.forward() tab.history.forward() expect([tab.ed.meta.value.id, tab.where()]).toEqual(['p2', 'top > leaf']) }) it('passes over circuits that were deleted since', async () => { const tab = await openTab() tab.ed.open('mid') tab.ed.open('leaf') tab.ed.goMain() tab.ed.project.value.components = tab.ed.project.value.components.filter(d => d.id !== 'leaf') tab.history.back() expect(tab.where()).toBe('top') }) })