Files
StarPilot/openpilot/starpilot/galaxy/tests/test_route_leave_async.mjs
T
firestar5683 00364fd645 Galaxy Access
Separate Galaxy authentication and local access policy from web presentation.

Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
2026-10-14 19:56:17 -05:00

22 lines
1.5 KiB
JavaScript

import assert from "node:assert/strict"
import fs from "node:fs"
const text=fs.readFileSync(new URL("../web/js/route-leave.js", import.meta.url),"utf8")
const {createRouteLeave}=await import("data:text/javascript;base64,"+Buffer.from(text).toString("base64"))
let entries=[{path:"/",index:0}],at=0,shown="/",pending,queue=[]
const port={read:()=>entries[at],show:()=>{shown=entries[at].path},
push:(path,index)=>{entries=entries.slice(0,at+1);entries.push({path,index});at++},
replace:(path,index)=>{entries[at]={path,index}},
go:delta=>queue.push(()=>{at+=delta;nav.changed();queue.push(()=>nav.changed())})}
const nav=createRouteLeave(port)
const flush=()=>{while(queue.length)queue.shift()()}
nav.navigate("/tools");nav.navigate("/theme_maker");nav.setGuard(callback=>{pending=callback})
port.go(-1);flush();assert.equal(shown,"/theme_maker");assert.equal(port.read().path,"/theme_maker")
nav.navigate("/settings");pending();assert.equal(shown,"/settings")
nav.setGuard(callback=>{pending=callback})
port.push("/logs",port.read().index);nav.changed();queue.push(()=>nav.changed());flush()
assert.equal(port.read().path,"/settings");assert.equal(shown,"/settings")
pending();flush();assert.equal(shown,"/logs")
port.go(-1);flush();assert.equal(shown,"/logs");pending();flush();assert.equal(shown,"/settings")
port.go(1);flush();assert.equal(shown,"/settings");pending();flush();assert.equal(shown,"/logs")
console.log("async Back/Forward, retained-state hash and canceled-then-new navigation passed")