mirror of
https://github.com/agent0ai/a0-launcher.git
synced 2026-12-05 01:39:50 +00:00
121 lines
5.3 KiB
JavaScript
121 lines
5.3 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const { after, test } = require('node:test');
|
|
const { trustedHostsFromRemoteInstances } = require('../remote_certificate_trust');
|
|
|
|
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'a0-launcher-certificate-trust-'));
|
|
const electronPath = require.resolve('electron');
|
|
const previousElectronModule = require.cache[electronPath];
|
|
require.cache[electronPath] = {
|
|
id: electronPath,
|
|
filename: electronPath,
|
|
loaded: true,
|
|
exports: {
|
|
app: { getPath: () => testRoot },
|
|
safeStorage: null
|
|
}
|
|
};
|
|
const stateStorePath = require.resolve('./state_store');
|
|
delete require.cache[stateStorePath];
|
|
const stateStore = require('./state_store');
|
|
|
|
after(() => {
|
|
fs.rmSync(testRoot, { recursive: true, force: true });
|
|
delete require.cache[stateStorePath];
|
|
if (previousElectronModule) require.cache[electronPath] = previousElectronModule;
|
|
else delete require.cache[electronPath];
|
|
});
|
|
|
|
async function saved(id) {
|
|
return (await stateStore.readRemoteInstances()).find((item) => item.id === id);
|
|
}
|
|
|
|
test('certificate trust is stored only when it is exactly true', async () => {
|
|
const trusted = await stateStore.writeRemoteInstance({
|
|
url: 'https://trusted.example.com:5443/',
|
|
allowUntrustedCertificate: true
|
|
});
|
|
assert.equal((await saved(trusted.id)).allowUntrustedCertificate, true);
|
|
|
|
for (const value of [false, 'true', 1, null]) {
|
|
const remote = await stateStore.writeRemoteInstance({
|
|
url: `https://value-${String(value)}.example.com/`,
|
|
allowUntrustedCertificate: value
|
|
});
|
|
assert.equal(Object.hasOwn(await saved(remote.id), 'allowUntrustedCertificate'), false);
|
|
}
|
|
});
|
|
|
|
test('certificate trust survives edits that do not mention it, and can be turned off', async () => {
|
|
const remote = await stateStore.writeRemoteInstance({
|
|
name: 'Lab',
|
|
url: 'https://lab.example.com:5443/',
|
|
allowUntrustedCertificate: true
|
|
});
|
|
|
|
await stateStore.writeRemoteInstance({ id: remote.id, name: 'Lab renamed', url: remote.url });
|
|
await stateStore.writeRemoteInstance({ id: remote.id, url: remote.url, color: '#336699' });
|
|
const edited = await saved(remote.id);
|
|
assert.equal(edited.name, 'Lab renamed');
|
|
assert.equal(edited.allowUntrustedCertificate, true);
|
|
|
|
await stateStore.writeRemoteInstance({ id: remote.id, url: remote.url, allowUntrustedCertificate: false });
|
|
assert.equal(Object.hasOwn(await saved(remote.id), 'allowUntrustedCertificate'), false);
|
|
});
|
|
|
|
test('an edit cannot move an Instance onto the URL of another one', async () => {
|
|
const first = await stateStore.writeRemoteInstance({ url: 'https://first.example.com/' });
|
|
const second = await stateStore.writeRemoteInstance({ url: 'https://second.example.com/' });
|
|
|
|
await assert.rejects(
|
|
stateStore.writeRemoteInstance({ id: second.id, url: 'https://first.example.com/' }),
|
|
/already uses this URL/
|
|
);
|
|
assert.equal((await saved(second.id)).url, 'https://second.example.com/');
|
|
|
|
// Adding the same URL again still updates the Instance that has it.
|
|
const again = await stateStore.writeRemoteInstance({ url: 'https://first.example.com/', name: 'First again' });
|
|
assert.equal(again.id, first.id);
|
|
assert.equal((await saved(first.id)).name, 'First again');
|
|
});
|
|
|
|
test('disabling trust clears shell and renderer caches without clearing credential metadata', async () => {
|
|
const remote = await stateStore.writeRemoteInstance({
|
|
url: 'https://cached.example.com/', allowUntrustedCertificate: true
|
|
});
|
|
const credentialMetadata = { saved: true, username: 'review-user' };
|
|
const current = { ...remote, launcherCredentials: credentialMetadata };
|
|
const published = [];
|
|
const context = vm.createContext({
|
|
stateStore,
|
|
_cachedState: { remoteInstances: [current] },
|
|
_refreshRequestSequence: 0,
|
|
instanceHealthCache: new Map(),
|
|
enrichRemoteInstancesWithHealth: value => value,
|
|
events: { emit: (_name, state) => published.push(state) },
|
|
store: { remoteInstances: [current] }
|
|
});
|
|
const manager = fs.readFileSync(path.join(__dirname, 'index.js'), 'utf8');
|
|
const renderer = fs.readFileSync(path.join(__dirname, '../../app/docker_manager.js'), 'utf8');
|
|
vm.runInContext(
|
|
manager.slice(manager.indexOf('function publishCachedState('), manager.indexOf('function backgroundOperationsSnapshot('))
|
|
+ manager.slice(manager.indexOf('async function updateRemoteInstance('), manager.indexOf('async function setRemoteInstanceCredentials('))
|
|
+ renderer.slice(renderer.indexOf('function upsertRemoteInstance('), renderer.indexOf('function patchLocalInstance(')),
|
|
context
|
|
);
|
|
for (const enabled of [false, true, false]) {
|
|
const updated = await context.updateRemoteInstance(remote.id, { allowUntrustedCertificate: enabled });
|
|
context.upsertRemoteInstance(updated);
|
|
context.patchCachedRemoteInstance(remote.id, { launcherCredentials: credentialMetadata });
|
|
for (const record of [context._cachedState.remoteInstances[0], context.store.remoteInstances[0]]) {
|
|
assert.equal(record.allowUntrustedCertificate, enabled);
|
|
assert.deepEqual(record.launcherCredentials, credentialMetadata);
|
|
}
|
|
assert.equal(trustedHostsFromRemoteInstances(published.at(-1).remoteInstances).has('cached.example.com'), enabled);
|
|
}
|
|
assert.equal(Object.hasOwn(await saved(remote.id), 'allowUntrustedCertificate'), false);
|
|
});
|