Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 202 additions & 3 deletions scripts/prepare-resources/bridge-bootstrap-updater-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,30 @@ function runBootstrap(source, authResults, sharedState = {}) {
sharedState.navigation = navigation;
const invocations = [];
const intervals = [];
const localStorage = createStorage(localValues);
const sessionStorage = createStorage(sessionValues);
class Storage {}
if (sharedState.nativeStorage) {
Object.assign(Storage.prototype, {
getItem(key) {
sharedState.beforeStorageRead?.(key);
return this.values.get(key) ?? null;
},
setItem(key, value) { this.values.set(String(key), String(value)); },
removeItem(key) { this.values.delete(String(key)); },
clear() { this.values.clear(); },
});
}
const makeStorage = (values) => {
if (!sharedState.nativeStorage) return createStorage(values);
const storage = Object.assign(new Storage(), { values });
if (sharedState.allowStorageOverrides) {
const { setItem, removeItem, clear } = Storage.prototype;
return Object.assign(storage, { setItem, removeItem, clear });
}
// Native Storage can reject instance-level method overrides.
return Object.preventExtensions(storage);
};
const localStorage = makeStorage(localValues);
const sessionStorage = makeStorage(sessionValues);
const location = {
href: 'http://127.0.0.1:6185/#/auth/login',
origin: 'http://127.0.0.1:6185',
Expand All @@ -53,10 +75,14 @@ function runBootstrap(source, authResults, sharedState = {}) {
},
};
const window = {
Storage: sharedState.nativeStorage ? Storage : undefined,
__TAURI_INTERNALS__: {
event: sharedState.event,
async invoke(command, payload = {}) {
invocations.push({ command, payload });
if (command === 'desktop_bridge_set_startup_theme') {
return sharedState.saveTheme?.(payload.theme);
}
if (command === 'desktop_bridge_install_app_update' && sharedState.install) {
return sharedState.install();
}
Expand All @@ -80,6 +106,9 @@ function runBootstrap(source, authResults, sharedState = {}) {
sessionStorage,
location,
open: () => null,
addEventListener: sharedState.addEventListener ?? (() => {}),
setTimeout: sharedState.setTimeout,
clearTimeout: sharedState.clearTimeout,
setInterval(handler, delay) {
intervals.push({ handler, delay });
return intervals.length;
Expand All @@ -88,6 +117,7 @@ function runBootstrap(source, authResults, sharedState = {}) {
class MockElement {}
class MockAnchor extends MockElement {}
const document = {
get readyState() { return sharedState.readyState; },
addEventListener() {},
getElementById(id) {
if (id !== 'app') return null;
Expand All @@ -104,7 +134,9 @@ function runBootstrap(source, authResults, sharedState = {}) {
source
.replace('{TRAY_RESTART_BACKEND_EVENT}', 'astrbot://tray-restart-backend')
.replace('{CHAT_TRANSPORT_MODE_STORAGE_KEY}', 'chat_transport_mode')
.replace('{CHAT_TRANSPORT_MODE_WEBSOCKET}', 'websocket'),
.replace('{CHAT_TRANSPORT_MODE_WEBSOCKET}', 'websocket')
.replace('{DASHBOARD_ORIGIN}', sharedState.dashboardOrigin ?? location.origin)
.replace('{STARTUP_THEME_REVISION}', String(sharedState.themeRevision ?? 1)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Revision injection regression goes unnoticed

When rust’s bridge injection stops replacing {STARTUP_THEME_REVISION}, runBootstrap replaces the revision placeholder itself, so the assertion passes even if Rust’s bridge injection stops supplying the native revision; the real request then carries an invalid revision and stale-write protection fails.

Test the revision substitution performed by Rust, or separately assert the bootstrap receives the production-injected script.

Prompt for AI agents
In `scripts/prepare-resources/bridge-bootstrap-updater-contract.test.mjs` at line 133:

**Revision injection regression goes unnoticed**

When rust’s bridge injection stops replacing `{STARTUP_THEME_REVISION}`, `runBootstrap` replaces the revision placeholder itself, so the assertion passes even if Rust’s bridge injection stops supplying the native revision; the real request then carries an invalid revision and stale-write protection fails.

Test the revision substitution performed by Rust, or separately assert the bootstrap receives the production-injected script.

{
window,
document,
Expand All @@ -126,6 +158,173 @@ function runBootstrap(source, authResults, sharedState = {}) {
};
}

function startupThemeRequests(invocations) {
return invocations
.filter(({ command }) => command === 'desktop_bridge_set_startup_theme')
.map(({ payload }) => payload.theme);
}

test('Dashboard reports its document revision only when its own load completes', async () => {
const source = await readFile(bootstrapPath, 'utf8');
const listeners = new Map();
const state = {
themeRevision: 7,
readyState: 'loading',
addEventListener: (event, handler) => listeners.set(event, handler),
};
const { invocations } = runBootstrap(source, [], state);
const readyRevisions = () => invocations
.filter(({ command }) => command === 'desktop_bridge_dashboard_loaded')
.map(({ payload }) => payload.revision);
assert.deepEqual(readyRevisions(), []);
state.readyState = 'complete';
listeners.get('load')?.();
assert.deepEqual(readyRevisions(), [7]);

const completed = runBootstrap(source, [], { themeRevision: 8, readyState: 'complete' });
assert.equal(completed.invocations.find(({ command }) => command === 'desktop_bridge_dashboard_loaded')
?.payload.revision, 8);

const other = runBootstrap(source, [], { dashboardOrigin: 'http://localhost:1420', readyState: 'complete' });
assert.equal(other.invocations.some(({ command }) => command === 'desktop_bridge_dashboard_loaded'), false);
});

test('startup theme requests carry the native revision of their Dashboard document', async () => {
const source = await readFile(bootstrapPath, 'utf8');
for (const themeRevision of [1, 2]) {
const { invocations } = runBootstrap(source, [], { themeRevision });
await flushAsyncWork();
const request = invocations.find(({ command }) => command === 'desktop_bridge_set_startup_theme');
assert.equal(request?.payload.revision, themeRevision);
}
});

for (const allowStorageOverrides of [false, true]) {
test(`bridge saves theme changes with ${allowStorageOverrides ? 'own' : 'prototype'} storage methods`, async () => {
const source = await readFile(bootstrapPath, 'utf8');
const { localStorage, sessionStorage, invocations } = runBootstrap(source, [], {
nativeStorage: true,
allowStorageOverrides,
localValues: new Map([['uiTheme', 'PurpleThemeDark']]),
});
for (const method of ['setItem', 'removeItem', 'clear']) {
assert.equal(Object.hasOwn(localStorage, method), allowStorageOverrides, method);
}
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark']);

sessionStorage.setItem('themeMode', 'light');
localStorage.setItem('themeMode', 'system');
localStorage.setItem('uiTheme', 'PurpleThemeDark');
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark', 'system']);

localStorage.setItem('themeMode', 'light');
await flushAsyncWork();
localStorage.removeItem('themeMode');
await flushAsyncWork();
localStorage.clear();
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark', 'system', 'light', 'dark', 'system']);
});
}

test('bridge waits for an in-flight theme save before sending the latest choice', async () => {
const source = await readFile(bootstrapPath, 'utf8');
let finishDarkSave;
const darkSave = new Promise((resolve) => { finishDarkSave = resolve; });
const { localStorage, invocations } = runBootstrap(source, [], {
nativeStorage: true,
localValues: new Map([['themeMode', 'dark']]),
saveTheme: (theme) => theme === 'dark' ? darkSave : undefined,
});
await flushAsyncWork();
localStorage.setItem('themeMode', 'system');
localStorage.setItem('themeMode', 'light');
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark']);

finishDarkSave();
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark', 'light']);
});

test('bridge retries repeated startup theme save failures until the save succeeds', async () => {
const source = await readFile(bootstrapPath, 'utf8');
const retryDelays = [1000, 2000, 4000, 8000, 16000, 30000, 30000];
let failuresRemaining = retryDelays.length;
const timers = [];
const { invocations } = runBootstrap(source, [], {
nativeStorage: true,
localValues: new Map([['themeMode', 'dark']]),
saveTheme() {
if (failuresRemaining-- > 0) throw new Error('temporary IPC failure');
},
setTimeout: (run, delay) => timers.push({ run, delay }),
});
await flushAsyncWork();
for (const [attempt, delay] of retryDelays.entries()) {
assert.deepEqual(startupThemeRequests(invocations), Array(attempt + 1).fill('dark'));
assert.deepEqual(timers.map((timer) => timer.delay), [delay]);
timers.shift().run();
await flushAsyncWork();
}
assert.deepEqual(startupThemeRequests(invocations), Array(retryDelays.length + 1).fill('dark'));
assert.equal(timers.length, 0);
});

test('bridge retries an interrupted theme storage read without a storage mutation', async () => {
const source = await readFile(bootstrapPath, 'utf8');
let failuresRemaining = 2;
const timers = [];
const { invocations } = runBootstrap(source, [], {
nativeStorage: true,
localValues: new Map([['themeMode', 'dark']]),
beforeStorageRead(key) {
if (key === 'themeMode' && failuresRemaining-- > 0) throw new Error('storage unavailable');
},
setTimeout: (run, delay) => timers.push({ run, delay }),
});
for (const delay of [1000, 2000]) {
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), []);
assert.deepEqual(timers.map((timer) => timer.delay), [delay]);
timers.shift().run();
}
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark']);
assert.equal(timers.length, 0);
});

test('bridge interrupts retry backoff when the Dashboard theme changes', async () => {
const source = await readFile(bootstrapPath, 'utf8');
const timers = new Map();
let nextTimerId = 0;
const { invocations, localStorage } = runBootstrap(source, [], {
nativeStorage: true,
localValues: new Map([['themeMode', 'dark']]),
saveTheme(theme) {
if (theme === 'dark') throw new Error('temporary IPC failure');
},
setTimeout(run) {
const id = ++nextTimerId;
timers.set(id, run);
return id;
},
clearTimeout: (id) => timers.delete(id),
});
await flushAsyncWork();
localStorage.setItem('uiTheme', 'PurpleThemeDark');
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark']);
assert.deepEqual([...timers.keys()], [1]);

localStorage.setItem('themeMode', 'light');
await flushAsyncWork();
assert.deepEqual(startupThemeRequests(invocations), ['dark', 'light']);
assert.equal(timers.size, 0);
});

test('bridge bootstrap defines astrbotAppUpdater methods', async () => {
const source = await readFile(bootstrapPath, 'utf8');

Expand Down
29 changes: 29 additions & 0 deletions scripts/prepare-resources/startup-shell-copy.test.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { runInNewContext } from 'node:vm';

const startupShellPath = new URL('../../ui/index.html', import.meta.url);
const startupCopyConfigPath = new URL('../../ui/startup-copy.js', import.meta.url);

test('startup shell applies an explicit theme before styles and follows system without a preference', async () => {
const source = await readFile(startupShellPath, 'utf8');
const beforeStyles = source.split('<style>')[0];
const themeScript = [...beforeStyles.matchAll(/<script>([\s\S]*?)<\/script>/g)]
.map((match) => match[1]).join('\n');

for (const [hash, prefersDark, expected] of [
['#startup-theme=dark', false, 'dark'],
['#startup-theme=light', true, 'light'],
['#startup-theme=unknown', true, 'dark'],
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
['#startup-theme=unknown', false, 'light'],
['', false, 'light'],
]) {
const dataset = {};
let onChange;
runInNewContext(themeScript, {
location: { hash },
document: { documentElement: { dataset } },
matchMedia: () => ({ matches: prefersDark, addEventListener: (_, listener) => { onChange = listener; } }),
});
assert.equal(dataset.startupTheme, expected, `${hash} with a ${prefersDark ? 'dark' : 'light'} system theme`);
if (hash === '') {
onChange({ matches: true });
assert.equal(dataset.startupTheme, 'dark');
}
}
});

test('startup shell loads shared copy config, reuses applyStartupMode, and exposes a focused live region', async () => {
const source = await readFile(startupShellPath, 'utf8');
const configSource = await readFile(startupCopyConfigPath, 'utf8').catch(() => '');
Expand Down
6 changes: 4 additions & 2 deletions src-tauri/src/app_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ static BACKEND_PATH_OVERRIDE: OnceLock<Option<OsString>> = OnceLock::new();
pub(crate) fn navigate_main_window_to_backend(
app_handle: &AppHandle,
cache_version: Option<&str>,
startup_page_loaded: bool,
) -> Result<(), String> {
let state = app_handle.state::<BackendState>();
window::main_window::navigate_main_window_to_backend(
app_handle,
&state.backend_url,
cache_version,
startup_page_loaded,
)
}

pub(crate) fn inject_desktop_bridge(webview: &tauri::Webview<tauri::Wry>) {
bridge::desktop::inject_desktop_bridge(webview, append_desktop_log);
pub(crate) fn inject_desktop_bridge(webview: &tauri::Webview<tauri::Wry>, page_url: &url::Url) {
bridge::desktop::inject_desktop_bridge(webview, page_url, append_desktop_log);
}

pub(crate) fn backend_path_override() -> Option<OsString> {
Expand Down
Loading