mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-11-08 13:33:42 +08:00
a82935f326
Integrate onroad views, configurable widgets, local settings, steering-profile contracts, Bluetooth and Android Auto with their device and runtime services. Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> Co-authored-by: Daniel Koepping <elkoled@gmail.com> Co-authored-by: Danny <dannyirwin31@yahoo.com> Co-authored-by: DimitriChrysafis <Dimitri.chrysafis@gmail.com> Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com> Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com> Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu> Co-authored-by: Shane Smiskol <shane@smiskol.com> Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com> Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com> Co-authored-by: stef <19478336+stefpi@users.noreply.github.com> Co-authored-by: whoisdomi <dcantu83@gmail.com> Co-authored-by: Intelli <contact@intelli.software> Co-authored-by: pharmacomaniac <blittle65@gmail.com> Co-authored-by: prabhaavp <prabhaav@msu.edu>
45 lines
2.3 KiB
JavaScript
45 lines
2.3 KiB
JavaScript
import assert from "node:assert/strict"
|
|
import { createHash, webcrypto } from "node:crypto"
|
|
import { decodeLayoutBackup, encodeLayoutBackup, sha256 } from "../web/js/layout-backup.js"
|
|
import { ToggleBackup } from "../web/js/toggle-backup.js"
|
|
|
|
const layout = { version: 4, palette: { text: "#FFFFFFFF" }, layouts: { large: {}, compact: {} } }
|
|
const encoded = await encodeLayoutBackup(layout, webcrypto)
|
|
assert.deepEqual(await decodeLayoutBackup(encoded, webcrypto), layout)
|
|
assert.deepEqual(await decodeLayoutBackup(encoded, {}), layout) // Plain HTTP has no crypto.subtle.
|
|
assert.equal(await encodeLayoutBackup(layout, {}), encoded)
|
|
for (const value of ["", "abc", "a".repeat(55), "a".repeat(56), "a".repeat(64), "a".repeat(1024),
|
|
"Café 🚗", "x".repeat(16384)]) {
|
|
const expected = createHash("sha256").update(value).digest("hex")
|
|
assert.equal(await sha256(value, {}), expected)
|
|
assert.equal(await sha256(value, webcrypto), expected)
|
|
}
|
|
assert.deepEqual(Object.keys(JSON.parse(encoded)), ["format", "version", "document", "sha256"])
|
|
assert.doesNotMatch(encoded, /calibration|credential|model/i)
|
|
|
|
for (const invalid of [
|
|
encoded.replace("#FFFFFFFF", "#000000FF"),
|
|
encoded.replace('"version":1', '"version":2'),
|
|
encoded.replace('"sha256":', '"extra":1,"sha256":'),
|
|
encoded.trimEnd(),
|
|
"x".repeat(32769),
|
|
]) {
|
|
await assert.rejects(decodeLayoutBackup(invalid, webcrypto))
|
|
}
|
|
|
|
// The page obtains a fresh revision immediately before the existing owner performs the restore.
|
|
const calls = []
|
|
const context = { draft: layout, legacy: true, busy: false, notice: "", error: "",
|
|
request: async (...args) => { calls.push(args); return args.length === 2 ? {valid:true} : {editable:true,revision:"current-revision"} } }
|
|
await ToggleBackup.methods.restoreBackup.call(context)
|
|
assert.deepEqual(calls, [["./api/ui/layout"], ["./api/ui/layout", {document:layout, revision:"current-revision"}]])
|
|
assert.equal(context.notice,"Layout and colors restored.")
|
|
assert.equal(context.draft,null)
|
|
let writes = 0
|
|
const moving = { draft:layout,legacy:true,busy:false,notice:"",error:"",
|
|
request:async () => {writes++;return {editable:false,revision:"old"}} }
|
|
await ToggleBackup.methods.restoreBackup.call(moving)
|
|
assert.equal(writes,1)
|
|
assert.match(moving.error,/Park/)
|
|
console.log("Portable layout files and legacy restore through Toggle backup passed")
|