Skip to content
Open
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
44 changes: 44 additions & 0 deletions src/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ const {
mockClose,
mockFindShadowedUserAdapters,
mockSetDaemonCommandTimeoutSeconds,
mockBinaryInfo,
mockEnsureBinary,
} = vi.hoisted(() => ({
mockGetDaemonHealth: vi.fn(),
mockConnect: vi.fn(),
mockClose: vi.fn(),
mockFindShadowedUserAdapters: vi.fn(),
mockSetDaemonCommandTimeoutSeconds: vi.fn(),
mockBinaryInfo: vi.fn(),
mockEnsureBinary: vi.fn(),
}));

vi.mock('cloakbrowser', () => ({
binaryInfo: mockBinaryInfo,
ensureBinary: mockEnsureBinary,
}));

vi.mock('./browser/daemon-transport.js', () => ({
Expand Down Expand Up @@ -46,6 +55,8 @@ describe('doctor report rendering', () => {
vi.clearAllMocks();
mockFindShadowedUserAdapters.mockReturnValue([]);
mockSetDaemonCommandTimeoutSeconds.mockClear();
mockBinaryInfo.mockReturnValue({ installed: true });
mockEnsureBinary.mockResolvedValue('/cache/cloak/Chromium');
// Doctor always runs live connectivity. Tests that want connect to fail override.
mockConnect.mockResolvedValue({
evaluate: vi.fn().mockResolvedValue(2),
Expand Down Expand Up @@ -290,6 +301,39 @@ describe('doctor report rendering', () => {
expect(mockSetDaemonCommandTimeoutSeconds).toHaveBeenLastCalledWith(null);
});

it('installs the CloakBrowser binary before starting the timed live probe', async () => {
mockBinaryInfo.mockReturnValueOnce({ installed: false });
let finishInstall!: () => void;
mockEnsureBinary.mockReturnValueOnce(new Promise<string>((resolve) => {
finishInstall = () => resolve('/cache/cloak/Chromium');
}));

const connectivity = checkConnectivity();
await vi.waitFor(() => expect(mockEnsureBinary).toHaveBeenCalledTimes(1));

expect(mockConnect).not.toHaveBeenCalled();
expect(mockSetDaemonCommandTimeoutSeconds).not.toHaveBeenCalled();

finishInstall();
await expect(connectivity).resolves.toMatchObject({ ok: true });
expect(mockConnect).toHaveBeenCalledTimes(1);
expect(mockSetDaemonCommandTimeoutSeconds).toHaveBeenNthCalledWith(1, 8);
expect(mockSetDaemonCommandTimeoutSeconds).toHaveBeenLastCalledWith(null);
});

it('reports binary installation failures without starting the daemon probe', async () => {
mockBinaryInfo.mockReturnValueOnce({ installed: false });
mockEnsureBinary.mockRejectedValueOnce(new Error('binary download failed'));

await expect(checkConnectivity()).resolves.toMatchObject({
ok: false,
error: 'binary download failed',
});
expect(mockConnect).not.toHaveBeenCalled();
expect(mockSetDaemonCommandTimeoutSeconds).toHaveBeenCalledTimes(1);
expect(mockSetDaemonCommandTimeoutSeconds).toHaveBeenCalledWith(null);
});

it('does not report an issue when the connected Cloak runtime does not report a version', async () => {
const status = {
state: 'ready' as const,
Expand Down
9 changes: 8 additions & 1 deletion src/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { DEFAULT_DAEMON_PORT } from './constants.js';
import { binaryInfo, ensureBinary } from 'cloakbrowser';
import { BrowserBridge } from './browser/index.js';
import { setDaemonCommandTimeoutSeconds } from './browser/daemon-client.js';
import { getDaemonHealth } from './browser/daemon-transport.js';
Expand Down Expand Up @@ -52,8 +53,14 @@ export type DoctorReport = {
export async function checkConnectivity(opts?: { timeout?: number }): Promise<ConnectivityResult> {
const start = Date.now();
const timeoutSeconds = opts?.timeout ?? DOCTOR_LIVE_TIMEOUT_SECONDS;
setDaemonCommandTimeoutSeconds(timeoutSeconds);
try {
// CloakBrowser installs Chromium lazily. Keep that potentially long first-use
// download outside the daemon's deliberately short live-probe deadline;
// otherwise doctor reports a timeout while the download keeps running and
// holds the profile command queue.
if (!binaryInfo().installed) await ensureBinary();

setDaemonCommandTimeoutSeconds(timeoutSeconds);
const bridge = new BrowserBridge();
const page = await bridge.connect({
timeout: timeoutSeconds,
Expand Down