2026-04-15 01:32:59 +08:00
|
|
|
import { describe, expect, test } from 'bun:test';
|
|
|
|
|
import {
|
|
|
|
|
resolveDesktopBootView,
|
|
|
|
|
canDismissInitialLoading,
|
|
|
|
|
getInjectedBootOutcome,
|
|
|
|
|
getBootInjectionStatus,
|
|
|
|
|
shouldRestartDesktopBootFlow,
|
|
|
|
|
} from './desktopBoot';
|
|
|
|
|
|
|
|
|
|
describe('resolveDesktopBootView', () => {
|
|
|
|
|
test('returns chooser for first launch (not-configured)', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: null, status: 'not-configured' },
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'chooser' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns recovery view for broken saved remote', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: {
|
|
|
|
|
target: 'remote',
|
|
|
|
|
status: 'unreachable',
|
|
|
|
|
hostId: 'remote-a',
|
|
|
|
|
url: 'https://x.test',
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'recovery', variant: 'remote-unreachable', hostId: 'remote-a', url: 'https://x.test' });
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-21 21:11:13 +03:00
|
|
|
test('preserves disabled local runtime capability for remote recovery', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: {
|
|
|
|
|
target: 'remote',
|
|
|
|
|
status: 'unreachable',
|
|
|
|
|
hostId: 'remote-a',
|
|
|
|
|
url: 'https://x.test',
|
|
|
|
|
localAvailable: false,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({
|
|
|
|
|
screen: 'recovery',
|
|
|
|
|
variant: 'remote-unreachable',
|
|
|
|
|
hostId: 'remote-a',
|
|
|
|
|
url: 'https://x.test',
|
|
|
|
|
localAvailable: false,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-15 01:32:59 +08:00
|
|
|
test('returns main for local ok', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: 'local', status: 'ok' },
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'main' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns main with hostId for remote ok', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: 'remote', status: 'ok', hostId: 'remote-1', url: 'https://example.com' },
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'main', hostId: 'remote-1', url: 'https://example.com' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns recovery-remote for remote wrong-service', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: {
|
|
|
|
|
target: 'remote',
|
|
|
|
|
status: 'wrong-service',
|
|
|
|
|
hostId: 'bad-host',
|
|
|
|
|
url: 'https://bad.test',
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'recovery', variant: 'remote-wrong-service', hostId: 'bad-host', url: 'https://bad.test' });
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-02 00:43:05 +03:00
|
|
|
test('returns recovery-remote for incompatible remote', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: {
|
|
|
|
|
target: 'remote',
|
|
|
|
|
status: 'incompatible',
|
|
|
|
|
hostId: 'old-host',
|
|
|
|
|
url: 'https://old.test',
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'recovery', variant: 'remote-incompatible', hostId: 'old-host', url: 'https://old.test' });
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-21 21:11:13 +03:00
|
|
|
test('returns chooser for local unreachable', () => {
|
2026-04-15 01:32:59 +08:00
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: 'local', status: 'unreachable' },
|
|
|
|
|
}),
|
2026-07-21 21:11:13 +03:00
|
|
|
).toEqual({ screen: 'chooser' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns remote-only chooser when local runtime is disabled', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: 'local', status: 'unreachable', localAvailable: false },
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'chooser', localAvailable: false });
|
2026-04-15 01:32:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns recovery view for remote missing', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: { target: 'remote', status: 'missing', hostId: 'gone-1' },
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({ screen: 'recovery', variant: 'remote-missing', hostId: 'gone-1' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null for non-desktop shell', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: false,
|
|
|
|
|
bootOutcome: { target: 'local', status: 'ok' },
|
|
|
|
|
}),
|
|
|
|
|
).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null when no boot outcome and desktop shell', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
bootOutcome: null,
|
|
|
|
|
}),
|
|
|
|
|
).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('canDismissInitialLoading', () => {
|
|
|
|
|
test('does not dismiss desktop loading before boot outcome is known', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismisses desktop when main outcome is known and initialized', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: true,
|
|
|
|
|
bootViewIsMain: true,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('does not dismiss desktop when main outcome is known but not initialized', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: false,
|
|
|
|
|
bootOutcomeKnown: true,
|
|
|
|
|
bootViewIsMain: true,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismisses desktop for non-main outcome without waiting for init', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: false,
|
|
|
|
|
bootOutcomeKnown: true,
|
|
|
|
|
bootViewIsMain: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('does not dismiss desktop for non-main outcome when outcome is not known', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: false,
|
|
|
|
|
bootViewIsMain: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismisses non-desktop when initialized', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: false,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('does not dismiss non-desktop when not initialized', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: false,
|
|
|
|
|
isInitialized: false,
|
|
|
|
|
bootOutcomeKnown: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('shouldRestartDesktopBootFlow', () => {
|
|
|
|
|
test('restarts the desktop app when boot UI is running in the startup window', () => {
|
|
|
|
|
expect(
|
|
|
|
|
shouldRestartDesktopBootFlow({
|
2026-06-03 02:42:00 +03:00
|
|
|
isDesktopShell: true,
|
2026-04-15 01:32:59 +08:00
|
|
|
isDesktopLocalOriginActive: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('does not restart when the local desktop origin is already active', () => {
|
|
|
|
|
expect(
|
|
|
|
|
shouldRestartDesktopBootFlow({
|
2026-06-03 02:42:00 +03:00
|
|
|
isDesktopShell: true,
|
2026-04-15 01:32:59 +08:00
|
|
|
isDesktopLocalOriginActive: true,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-03 02:42:00 +03:00
|
|
|
test('does not restart outside the desktop shell', () => {
|
2026-04-15 01:32:59 +08:00
|
|
|
expect(
|
|
|
|
|
shouldRestartDesktopBootFlow({
|
2026-06-03 02:42:00 +03:00
|
|
|
isDesktopShell: false,
|
2026-04-15 01:32:59 +08:00
|
|
|
isDesktopLocalOriginActive: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getInjectedBootOutcome', () => {
|
|
|
|
|
// Bun test runner does not provide `window`. Mock it for these tests.
|
|
|
|
|
const mockWindow = () => {
|
|
|
|
|
const w: Record<string, unknown> = {};
|
|
|
|
|
(globalThis as Record<string, unknown>).window = w;
|
|
|
|
|
return w;
|
|
|
|
|
};
|
|
|
|
|
const restoreWindow = () => {
|
|
|
|
|
delete (globalThis as Record<string, unknown>).window;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test('returns null when window global is undefined', () => {
|
|
|
|
|
delete (globalThis as Record<string, unknown>).window;
|
|
|
|
|
try {
|
|
|
|
|
expect(getInjectedBootOutcome()).toBeNull();
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null for malformed payload with unknown kind', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { kind: 'unknown-kind' };
|
|
|
|
|
try {
|
|
|
|
|
expect(getInjectedBootOutcome()).toBeNull();
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null for payload missing required hostId', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { kind: 'main-remote', url: 'https://x.test' };
|
|
|
|
|
try {
|
|
|
|
|
expect(getInjectedBootOutcome()).toBeNull();
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null for non-object payload', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = 'not-an-object';
|
|
|
|
|
try {
|
|
|
|
|
expect(getInjectedBootOutcome()).toBeNull();
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns valid outcome for well-formed main-local', () => {
|
|
|
|
|
const w = mockWindow();
|
2026-04-26 16:24:07 +03:00
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { target: 'local', status: 'ok' };
|
2026-04-15 01:32:59 +08:00
|
|
|
try {
|
2026-04-26 16:24:07 +03:00
|
|
|
expect(getInjectedBootOutcome()).toEqual({ target: 'local', status: 'ok' });
|
2026-04-15 01:32:59 +08:00
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns null for payload with numeric kind', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { kind: 42 };
|
|
|
|
|
try {
|
|
|
|
|
expect(getInjectedBootOutcome()).toBeNull();
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('resolveDesktopBootView validation', () => {
|
|
|
|
|
test('returns null for unknown kind via default branch', () => {
|
|
|
|
|
expect(
|
|
|
|
|
resolveDesktopBootView({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
// @ts-expect-error — testing unknown kind
|
|
|
|
|
bootOutcome: { kind: 'totally-unknown' },
|
|
|
|
|
}),
|
|
|
|
|
).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getBootInjectionStatus', () => {
|
|
|
|
|
const mockWindow = () => {
|
|
|
|
|
const w: Record<string, unknown> = {};
|
|
|
|
|
(globalThis as Record<string, unknown>).window = w;
|
|
|
|
|
return w;
|
|
|
|
|
};
|
|
|
|
|
const restoreWindow = () => {
|
|
|
|
|
delete (globalThis as Record<string, unknown>).window;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test('returns "not-injected" when window is undefined', () => {
|
|
|
|
|
delete (globalThis as Record<string, unknown>).window;
|
|
|
|
|
try {
|
|
|
|
|
expect(getBootInjectionStatus()).toBe('not-injected');
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns "not-injected" when global is absent', () => {
|
|
|
|
|
mockWindow();
|
|
|
|
|
// Do not set the global — it should be absent.
|
|
|
|
|
try {
|
|
|
|
|
expect(getBootInjectionStatus()).toBe('not-injected');
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns "not-injected" when global is explicitly null', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = null;
|
|
|
|
|
try {
|
|
|
|
|
expect(getBootInjectionStatus()).toBe('not-injected');
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns "malformed" when global is present but invalid', () => {
|
|
|
|
|
const w = mockWindow();
|
|
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { kind: 'bad' };
|
|
|
|
|
try {
|
|
|
|
|
expect(getBootInjectionStatus()).toBe('malformed');
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('returns "valid" when global is present and well-formed', () => {
|
|
|
|
|
const w = mockWindow();
|
2026-04-26 16:24:07 +03:00
|
|
|
w.__OPENCHAMBER_DESKTOP_BOOT_OUTCOME__ = { target: 'local', status: 'ok' };
|
2026-04-15 01:32:59 +08:00
|
|
|
try {
|
|
|
|
|
expect(getBootInjectionStatus()).toBe('valid');
|
|
|
|
|
} finally {
|
|
|
|
|
restoreWindow();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('canDismissInitialLoading with malformed injection', () => {
|
|
|
|
|
test('does NOT dismiss desktop splash when injection is malformed', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: false,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('dismisses desktop main outcome when valid and initialized', () => {
|
|
|
|
|
expect(
|
|
|
|
|
canDismissInitialLoading({
|
|
|
|
|
isDesktopShell: true,
|
|
|
|
|
isInitialized: true,
|
|
|
|
|
bootOutcomeKnown: true,
|
|
|
|
|
bootViewIsMain: true,
|
|
|
|
|
}),
|
|
|
|
|
).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|