Files
DeepTutor/apps/desktop/test/runtime.test.mjs
Bingxi Zhao (Frank) a4fa63b6e2 feat(desktop): add Open DeepTutor for macOS under apps/desktop
Move the Electron macOS app into the main repository and ship it with every
DeepTutor release.

- apps/desktop: Electron shell that installs a signed bundled runtime and
  hosts the web UI; versioned with the bundled core.
- White app icon with the black DeepTutor mark, generated by
  scripts/make-icon.swift; redesigned start screen and title bar.
- Shell copy in en/zh/de/fr/pl/uk, starting in English and following the
  interface language and theme from DeepTutor Settings, including live changes.
- Web UI labels itself "Open DeepTutor MacOS v<version>" inside the app
  (user-agent marker) and "Open DeepTutor Web v<version>" in a browser.
- Sealing refuses a missing or mismatched signing key instead of silently
  generating a new one.
- desktop-release.yml builds, smoke-tests and attaches the DMG to each
  GitHub Release; Developer ID signing and notarization run when secrets exist.
- tests.yml runs the shell checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 19:20:15 +08:00

91 lines
5.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { generateKeyPairSync, sign } from 'node:crypto';
import http from 'node:http';
import { mkdtemp, mkdir, writeFile, readFile, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { localURL, externalURL, validateConfig } from '../src/config.mjs';
import { canonicalManifest, verifyManifest } from '../src/bundles.mjs';
import { ProcessGroup, availablePort, probe, waitReady } from '../src/processes.mjs';
import { SourceRuntime, stageBundledWeb } from '../src/runtime.mjs';
test('only explicit loopback origins are accepted for attachment', () => {
assert.equal(localURL('http://127.0.0.1:3782/'), 'http://127.0.0.1:3782');
for (const url of ['https://example.com', 'http://localhost.attacker.com', 'http://user@localhost:80', 'file:///tmp/test', 'http://localhost/?token=foo']) {
assert.throws(() => localURL(url));
}
assert.equal(externalURL('file:///etc/passwd'), false);
assert.equal(externalURL('https://deeptutor.info/'), true);
});
test('source data cannot share the checkout', () => {
const config = { schemaVersion: 1, mode: 'source', sourcePath: '/tmp/core', pythonPath: '/python', nodePath: '/node', runtimeHome: '/tmp/core/data' };
assert.throws(() => validateConfig(config));
assert.equal(validateConfig({ ...config, runtimeHome: '/tmp/data' }).runtimeHome, '/tmp/data');
});
test('update signatures bind version, checksum and architecture', () => {
const { privateKey, publicKey } = generateKeyPairSync('ed25519');
const manifest = { formatVersion: 1, protocolVersion: 1, platform: 'darwin', arch: 'arm64', runtimeId: '1.7.0-test-arm64', archive: 'core.tar.gz', sha256: 'a'.repeat(64) };
manifest.signature = sign(null, Buffer.from(canonicalManifest(manifest)), privateKey).toString('base64');
assert.equal(verifyManifest(manifest, publicKey, 'arm64'), manifest);
assert.throws(() => verifyManifest({ ...manifest, sha256: 'b'.repeat(64) }, publicKey, 'arm64'));
assert.throws(() => verifyManifest(manifest, publicKey, 'x64'));
assert.throws(() => verifyManifest({ ...manifest, runtimeId: '../outside' }, publicKey, 'arm64'));
assert.throws(() => verifyManifest({ ...manifest, archive: '../core.tar.gz' }, publicKey, 'arm64'));
});
test('occupied ports are left alone and an available port is chosen', async t => {
const server = http.createServer((_req, res) => res.end('ready'));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => server.close());
const occupied = server.address().port;
assert.notEqual(await availablePort(occupied), occupied);
assert.equal(await probe(`http://127.0.0.1:${occupied}`), true);
});
test('stopping attach mode never stops the attached server', async t => {
const server = http.createServer((_req, res) => res.end('ready'));
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => server.close());
const url = `http://127.0.0.1:${server.address().port}`;
const runtime = new SourceRuntime();
await runtime.start({ schemaVersion: 1, mode: 'attach', url }, '/unused');
assert.equal(runtime.state.phase, 'ready');
await runtime.stop();
assert.equal(await probe(url), true);
});
test('process owner terminates a real child and rejects nonzero commands', async () => {
const group = new ProcessGroup();
await assert.rejects(group.run(process.execPath, ['-e', 'process.exit(7)']), /7/);
const port = await availablePort();
const child = group.spawn(process.execPath, ['-e', `require('http').createServer((q,s)=>s.end('ok')).listen(${port},'127.0.0.1')`]);
await waitReady(`http://127.0.0.1:${port}`, { timeout: 5000 });
await group.stop();
assert.equal(group.alive(child), false);
assert.equal(await probe(`http://127.0.0.1:${port}`), false);
});
test('readiness polling can be cancelled', async () => {
const controller = new AbortController();
controller.abort();
await assert.rejects(waitReady('http://127.0.0.1:1', { signal: controller.signal }), /abort/i);
});
test('public placeholders are patched in a cache, preserving immutable bundle', async t => {
const root = await mkdtemp(path.join(os.tmpdir(), 'Open DeepTutor test '));
t.after(() => rm(root, { recursive: true, force: true }));
const source = path.join(root, 'source');
await mkdir(path.join(source, '.next/static'), { recursive: true });
const contents = '__NEXT_PUBLIC_API_BASE_PLACEHOLDER__ __NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__';
await writeFile(path.join(source, 'server.js'), contents);
await writeFile(path.join(source, '.next/static/client.js'), contents);
const cache = await stageBundledWeb(source, path.join(root, 'cache'), 'http://127.0.0.1:1001', 'false');
assert.equal(await readFile(path.join(cache, 'server.js'), 'utf8'), 'http://127.0.0.1:1001 false');
assert.equal(await readFile(path.join(source, 'server.js'), 'utf8'), contents);
await stageBundledWeb(source, path.join(root, 'cache'), 'http://127.0.0.1:1002', 'true');
assert.equal(await readFile(path.join(cache, '.next/static/client.js'), 'utf8'), 'http://127.0.0.1:1002 true');
});