🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import fs from 'node:fs';
import { createRequire } from 'node:module';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const mainPath = fileURLToPath(new URL('../main.js', import.meta.url));
const loaderPath = fileURLToPath(new URL('../core-loader.js', import.meta.url));
const rescuePath = fileURLToPath(new URL('../rescue/index.js', import.meta.url));
const updatePath = fileURLToPath(new URL('../update/index.js', import.meta.url));
const abiPath = fileURLToPath(new URL('../abi.json', import.meta.url));
const require = createRequire(mainPath);
const electronPath = require.resolve('electron');
let tmp;
let resourcesPath;
let coreMain;
let wroteAbi = false;
const stub = (file, exports) => {
require.cache[file] = { exports, id: file, loaded: true };
};
const loadMain = ({ app, electron = {}, resolveCore, runRescue = vi.fn() }) => {
delete require.cache[updatePath];
stub(electronPath, { app, ...electron });
if (resolveCore) stub(loaderPath, { installShellResolver: vi.fn(), resolveCore });
stub(rescuePath, { runRescue });
delete require.cache[mainPath];
delete require.cache[coreMain];
require(mainPath);
return { runRescue };
};
const packagedApp = (overrides = {}) => ({
exit: vi.fn(),
getPath: vi.fn(() => path.join(tmp, 'userData')),
getVersion: () => '1.0.0',
isPackaged: true,
relaunch: vi.fn(),
requestSingleInstanceLock: vi.fn(() => true),
whenReady: vi.fn(() => Promise.resolve()),
...overrides,
});
const fakeCore = (source, body = 'module.exports = 1;') => {
fs.mkdirSync(path.dirname(coreMain), { recursive: true });
fs.writeFileSync(coreMain, body);
return {
dir: path.join(tmp, 'core'),
log: ['a log line'],
manifest: null,
markBroken: vi.fn(),
markHealthy: vi.fn(),
startBoot: vi.fn(),
source,
};
};
beforeEach(() => {
// abi.json is generated by beforePack and gitignored, so a clean checkout has none
if (!fs.existsSync(abiPath)) {
fs.writeFileSync(
abiPath,
JSON.stringify({ publicKey: '', shellAbi: 'test', shellVersion: '1.0.0' }),
);
wroteAbi = true;
}
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'shell-main-'));
coreMain = path.join(tmp, 'core', 'dist', 'main', 'index.js');
resourcesPath = process.resourcesPath;
process.resourcesPath = path.join(tmp, 'resources');
fs.mkdirSync(path.join(process.resourcesPath, 'core.asar'), { recursive: true });
fs.writeFileSync(
path.join(process.resourcesPath, 'core.asar/manifest.json'),
JSON.stringify({ channel: 'stable' }),
);
});
afterEach(() => {
for (const file of [electronPath, loaderPath, rescuePath, mainPath, coreMain, abiPath])
delete require.cache[file];
if (wroteAbi) {
fs.rmSync(abiPath, { force: true });
wroteAbi = false;
}
delete global.__SHELL__;
process.resourcesPath = resourcesPath;
fs.rmSync(tmp, { force: true, recursive: true });
});
describe('shell main', () => {
it('exits a secondary instance before touching core OTA boot state', () => {
const userData = path.join(tmp, 'userData');
const app = {
exit: vi.fn(),
getPath: vi.fn(() => userData),
getVersion: () => '1.0.0',
isPackaged: true,
requestSingleInstanceLock: vi.fn(() => false),
};
stub(electronPath, { app });
delete require.cache[mainPath];
require(mainPath);
expect(app.exit).toHaveBeenCalledWith(0);
expect(app.getPath).not.toHaveBeenCalled();
expect(fs.existsSync(path.join(userData, 'core-ota', 'boot.json'))).toBe(false);
});
it('boots the resolved core and exposes markHealthy to it', () => {
const core = fakeCore('builtin', 'global.__BOOTED__ = true;');
const { runRescue } = loadMain({ app: packagedApp(), resolveCore: () => core });
expect(global.__BOOTED__).toBe(true);
global.__SHELL__.markHealthy();
expect(core.markHealthy).toHaveBeenCalledOnce();
expect(runRescue).not.toHaveBeenCalled();
delete global.__BOOTED__;
});
it('starts stable boot validation without waiting for the background security check', async () => {
const core = fakeCore('builtin');
const resolveCore = vi.fn(({ deferBoot }) => {
if (!deferBoot) core.startBoot();
return core;
});
loadMain({ app: packagedApp(), resolveCore });
const check = vi.fn();
let release;
const background = global.__SHELL__.startupUpdate.checkSecurity({
check: () =>
new Promise((resolve) => {
release = resolve;
}),
});
expect(resolveCore).toHaveBeenCalledWith(expect.objectContaining({ deferBoot: false }));
expect(core.startBoot).toHaveBeenCalledOnce();
expect(await global.__SHELL__.startupUpdate.run(check)).toBe(true);
expect(check).not.toHaveBeenCalled();
expect(core.startBoot).toHaveBeenCalledOnce();
release(false);
expect(await background).toBe(true);
expect(core.startBoot).toHaveBeenCalledOnce();
});
it.each([
['stable', false],
['canary', true],
['beta', true],
])('counts early interrupted %s launches (existing: %s)', (channel, existing) => {
const builtinDir = path.join(process.resourcesPath, 'core.asar');
fs.writeFileSync(
path.join(builtinDir, 'manifest.json'),
JSON.stringify({ channel, version: '1.0.0' }),
);
const userData = path.join(tmp, 'userData');
fs.mkdirSync(userData, { recursive: true });
if (existing) fs.writeFileSync(path.join(userData, 'lobehub-settings.json'), '{}');
// Never enter bootstrap or mark healthy, simulating interruption before App starts.
coreMain = path.join(builtinDir, 'dist/main/index.js');
fs.mkdirSync(path.dirname(coreMain), { recursive: true });
fs.writeFileSync(coreMain, 'module.exports = 1;');
delete require.cache[loaderPath];
const { resolveCore } = require(loaderPath);
for (let attempt = 1; attempt <= 3; attempt += 1) {
loadMain({ app: packagedApp(), resolveCore });
const boot = JSON.parse(fs.readFileSync(path.join(userData, 'core-ota/boot.json'), 'utf8'));
expect(boot.failures).toBe(attempt);
}
const { runRescue } = loadMain({ app: packagedApp(), resolveCore });
expect(runRescue).toHaveBeenCalledOnce();
});
it.each(['canary', 'beta'])(
'defers a fresh %s boot until the gate allows entry',
async (channel) => {
fs.writeFileSync(
path.join(process.resourcesPath, 'core.asar/manifest.json'),
JSON.stringify({ channel }),
);
const core = fakeCore('builtin');
const resolveCore = vi.fn(({ deferBoot }) => {
if (!deferBoot) core.startBoot();
return core;
});
loadMain({ app: packagedApp(), electron: { net: { isOnline: () => false } }, resolveCore });
expect(core.startBoot).not.toHaveBeenCalled();
expect(await global.__SHELL__.startupUpdate.run(vi.fn())).toBe(true);
expect(core.startBoot).toHaveBeenCalledOnce();
},
);
it('rescues in-process when the builtin core throws while loading', () => {
const core = fakeCore('builtin', 'throw new Error("builtin boom");');
const { runRescue } = loadMain({ app: packagedApp(), resolveCore: () => core });
expect(core.markBroken).toHaveBeenCalled();
expect(runRescue).toHaveBeenCalledWith(
expect.objectContaining({
downloadUrl: 'https://lobehub.com/downloads',
error: expect.objectContaining({ message: 'builtin boom' }),
log: ['a log line'],
}),
);
});
it('relaunches instead of rescuing when an external core throws while loading', () => {
const app = packagedApp();
const core = fakeCore('external', 'throw new Error("external boom");');
const { runRescue } = loadMain({ app, resolveCore: () => core });
expect(core.markBroken).toHaveBeenCalled();
expect(app.relaunch).toHaveBeenCalled();
expect(app.exit).toHaveBeenCalledWith(1);
expect(runRescue).not.toHaveBeenCalled();
});
it('rescues without loading any core when the loader asks for it', () => {
const core = fakeCore('rescue', 'global.__BOOTED__ = true;');
const { runRescue } = loadMain({ app: packagedApp(), resolveCore: () => core });
expect(global.__BOOTED__).toBeUndefined();
expect(runRescue).toHaveBeenCalledWith(expect.objectContaining({ log: ['a log line'] }));
});
it('rescues when the shell itself throws before a core is resolved', () => {
const { runRescue } = loadMain({
app: packagedApp(),
resolveCore: () => {
throw new Error('loader boom');
},
});
expect(runRescue).toHaveBeenCalledWith(
expect.objectContaining({ error: expect.objectContaining({ message: 'loader boom' }) }),
);
});
it('falls back to the download page when the rescue module itself fails', async () => {
const app = packagedApp();
const dialog = { showErrorBox: vi.fn() };
const shell = { openExternal: vi.fn(() => Promise.resolve()) };
const core = fakeCore('rescue');
loadMain({
app,
electron: { dialog, shell },
resolveCore: () => core,
runRescue: () => {
throw new Error('rescue boom');
},
});
await vi.waitFor(() => expect(app.exit).toHaveBeenCalledWith(1));
expect(dialog.showErrorBox).toHaveBeenCalled();
expect(shell.openExternal).toHaveBeenCalledWith('https://lobehub.com/downloads');
});
});