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
191 changes: 102 additions & 89 deletions web/app/runners.js

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions web/i18n-en.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ <h1>我的知识库</h1>
<div class="runner-dispatch-copy">
<div class="runner-dispatch-head">
<span class="runner-dispatch-label" id="runnerDispatchLabel">当前执行调度</span>
<p class="runner-pool-status" id="runnerPoolStatus" aria-live="polite"></p>
<p class="runner-pool-status" id="runnerPoolStatus" aria-live="polite" data-i18n-data></p>
</div>
<p class="runner-dispatch-hint" id="runnerDispatchHint" hidden></p>
</div>
Expand Down
4 changes: 3 additions & 1 deletion web/run_queue_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function source(name) {
return next ? tail.slice(0, next.index) : tail;
}
function load(context, names) {
// The page copy is Chinese unless i18n.js switched the page to English.
if (!('lfT' in context)) context.lfT = zh => zh;
vm.createContext(context);
for (const name of names) vm.runInContext(source(name), context);
return context;
Expand Down Expand Up @@ -140,7 +142,7 @@ async function main() {
auto.concurrencyExempt=true;
assert.match(machine.runExecutionChipHTML(auto),/自动分配 · Team machine/);

const limitUI=load({escapeHTML:s=>String(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]))},['runnerRunLimitHTML']);
const limitUI=load({escapeHTML:s=>String(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]))},['platformRunnerLabel','runnerRunLimitHTML']);
assert.equal(limitUI.runnerRunLimitHTML({id:'cloud',ownerUserId:0}),'','platform machines have no per-machine limit');
const unlimited=limitUI.runnerRunLimitHTML({id:'m',name:'mac',ownerUserId:9});
assert.match(unlimited,/placeholder="不限" value=""/);
Expand Down
4 changes: 2 additions & 2 deletions web/runner_default_rule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function extract(name) {
return code.slice(start, code.indexOf('\n}', start) + 2);
}

const context = {state: {minRunnerVersion: '0.8.0'}};
const context = {state: {minRunnerVersion: '0.8.0'}, lfT: zh => zh};
vm.createContext(context);
vm.runInContext(extract('runnerVersionAtLeast') + extract('runnerDefaultBlocker'), context);
const blocker = r => context.runnerDefaultBlocker(r);
Expand All @@ -24,5 +24,5 @@ context.state.minRunnerVersion = '';
assert.equal(blocker({status: 'online', runnerVersion: '0.1.0'}), '');

assert.ok(code.includes('${defaultBlocker?`disabled title='), 'set-default button must use runnerDefaultBlocker');
assert.ok(code.includes("Runner ${escapeHTML(r.runnerVersion||'未知')}"), 'card shows the runner version');
assert.ok(code.includes("Runner ${escapeHTML(r.runnerVersion||lfT('未知','unknown'))}"), 'card shows the runner version');
console.log('runner default rule ok');
4 changes: 3 additions & 1 deletion webtest/lib/load-app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,16 @@ class ObserverStub {
takeRecords() { return []; }
}

export async function loadApp({ routes = [], hash = '', dispatchReady = true, origin = 'http://127.0.0.1' } = {}) {
export async function loadApp({ routes = [], hash = '', dispatchReady = true, origin = 'http://127.0.0.1', lang = '' } = {}) {
const html = await readFile(path.join(WEB_ROOT, 'index.html'), 'utf8');
const dom = new JSDOM(html, {
runScripts: 'outside-only',
pretendToBeVisual: true,
url: `${origin}/${hash ? `#${hash.replace(/^#/, '')}` : ''}`,
});
const { window } = dom;
// boot.js reads the saved language before the app scripts run.
if (lang) window.localStorage.setItem('loopforge.lang', lang);
const router = createFetchRouter(routes);

window.fetch = router.fetch;
Expand Down
168 changes: 168 additions & 0 deletions webtest/tests/i18n-runners.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { loadApp, settle } from '../lib/load-app.mjs';

const HAN = /[㐀-鿿]/;
const ATTRS = ['placeholder', 'title', 'aria-label', 'data-tip', 'data-tooltip'];

const runner = {
id: 'runner-mac', name: 'mac-mini', ownerUserId: 1, status: 'online', os: 'darwin', arch: 'arm64',
runnerVersion: '0.9.0', runLimit: 10, lastSeenAt: new Date().toISOString(), canUseInProject: true,
allowPrivilegedSandbox: false, sharedProjectIds: ['p1'],
load: { runningRuns: 1, occupiedRuns: 2, queuedRuns: 1 },
runtimes: [
{ provider: 'codex', status: 'online', authMode: 'local', version: '1.0' },
{ provider: 'opencode', status: 'online', authMode: 'personal', version: '0.5' },
],
};
const offline = { ...runner, id: 'runner-old', name: 'old-box', status: 'offline', runnerVersion: '0.1.0', runtimes: [], sharedProjectIds: [] };
const shared = { ...runner, id: 'runner-team', name: 'team-box', ownerUserId: 2, runtimes: [runner.runtimes[0]] };
const platformRunner = { name: '平台执行机器(云端)', status: 'online', runLimit: 10, canEditLimit: true, allowPrivilegedSandbox: true, load: { runningRuns: 0, occupiedRuns: 1 } };

async function runnersPage(lang, defaults) {
const app = await loadApp({ lang });
app.router.post('/api/runners/bind-codes', { code: 'BIND123', expiresAt: '2099-01-01T00:00:00Z' });
app.router.get('/api/runners?projectId=p1', [runner, offline, shared]);
app.router.get('/api/runners/default?projectId=p1', { minRunnerVersion: '0.8.0', platformRunner, ...defaults });
app.evaluate(`
state.currentUser={id:1,loginName:'owner'};
state.data.users=[{id:1,loginName:'owner',codexApiKeyConfigured:true}];
state.data.projects=[{id:'p1',name:'Shop',ownerId:1,members:[]}];
state.projectFilter='p1';
document.querySelector('#runnersView').classList.add('active');
`);
await app.evaluate('refreshRunners(true)');
await settle(6);
return app;
}

// What the English page still shows in Chinese once i18n.js has rewritten the
// text the app rendered (its MutationObserver is stubbed out in these tests).
function leftoverChinese(window, root) {
const left = [];
for (const el of [root, ...root.querySelectorAll('*')]) {
if (el.closest('[data-i18n-skip]')) continue;
for (const node of el.childNodes) {
if (node.nodeType === 3 && HAN.test(window.lfTranslate(node.nodeValue))) left.push(node.nodeValue.trim());
}
for (const name of ATTRS) {
const value = el.getAttribute(name);
if (value && HAN.test(window.lfTranslate(value))) left.push(`${name}="${value}"`);
}
}
return left;
}

for (const [mode, defaults] of [
['personal default', { runnerId: 'runner-mac', personalRunnerId: 'runner-mac' }],
['project default', { runnerId: 'runner-mac', projectRunnerId: 'runner-mac' }],
['auto-assign', { projectRunnerMode: 'auto' }],
['offline project default', { runnerId: 'runner-old', projectRunnerId: 'runner-old' }],
]) {
test(`English 执行机器 page leaves no Chinese (${mode})`, async (t) => {
const app = await runnersPage('en', defaults);
t.after(app.close);
const view = app.document.querySelector('#runnersView');
assert.match(view.querySelector('#runnersList').textContent, /mac-mini/);
assert.deepEqual(leftoverChinese(app.window, view), []);
});
}

test('English 执行机器 page renders whole English phrases, not mixed fragments', async (t) => {
const app = await runnersPage('en', { runnerId: 'runner-mac', personalRunnerId: 'runner-mac' });
t.after(app.close);
const { document } = app;
const limit = document.querySelector('.runner-platform-card .runner-run-limit label');
// The label wraps the input; CSS gap spaces the words around it.
assert.deepEqual([...limit.childNodes].filter(n => n.nodeType === 3).map(n => n.nodeValue), ['Max concurrent', 'runs']);
assert.equal(limit.querySelector('input').placeholder, 'Unlimited');
assert.match(document.querySelector('.runner-platform-card .runner-tip').dataset.tip, /^Defaults to 10, shared by every project on the platform\. .*Enter 0 or leave blank for no limit\.$/);
assert.equal(document.querySelector('.runner-platform-card .runner-load').textContent, 'Running 0 · Occupied 1 / 10');
assert.equal(document.querySelector('.runner-platform-card .runner-card-name b').textContent, 'Platform runner (cloud)');
assert.equal(document.querySelector('#runnerPoolStatus').textContent, 'Personal default · mac-mini');
assert.equal(app.evaluate(`runnerConcurrencyLabel({runLimit:1})`), 'Up to 1 concurrent run');
assert.equal(app.evaluate(`runnerDefaultBlocker({status:'online',runnerVersion:'0.1.0'})`), 'Runner version 0.1.0 is too old; upgrade to 0.8.0 or later');
assert.equal(app.evaluate(`runnerLastSeen(new Date(Date.now()-120000).toISOString())`), '2 min ago');
for (const text of [app.evaluate('runnerKeyDescription("zcode")'), app.evaluate('runnerKeyDescription("pi")'),
app.evaluate('runnerAuthDescription.local'), app.evaluate('runnerAuthDescription.personal')]) {
assert.doesNotMatch(text, HAN, text);
}
});

test('English 执行机器 dialogs: share, remove and the auth help are English', async (t) => {
const app = await runnersPage('en', { runnerId: 'runner-mac', personalRunnerId: 'runner-mac' });
t.after(app.close);
const { document } = app;
document.querySelector('[data-runner-auth-help]').click();
await settle(2);
assert.equal(document.querySelector('#confirmTitle').textContent, 'Model authentication');
assert.doesNotMatch(document.querySelector('#confirmMessage').textContent, HAN);
document.querySelector('#confirmOkBtn').click();
await settle(4);
app.evaluate(`window.__share=editRunnerSharing(state.runners[0])`);
await settle(2);
assert.equal(document.querySelector('#confirmTitle').textContent, 'Share mac-mini');
assert.doesNotMatch(document.querySelector('#confirmMessage').textContent, HAN);
document.querySelector('#confirmCancelBtn').click();
await app.evaluate('window.__share');
await settle(4);
app.evaluate(`window.__remove=removeRunner('runner-old')`);
await settle(2);
assert.equal(document.querySelector('#confirmTitle').textContent, 'Remove runner');
assert.match(document.querySelector('#confirmMessage').textContent, /^Remove old-box\?/);
document.querySelector('#confirmCancelBtn').click();
await app.evaluate('window.__remove');
});

test('English 执行机器 page keeps runner and project names as typed', async (t) => {
const app = await loadApp({ lang: 'en' });
t.after(app.close);
app.router.post('/api/runners/bind-codes', { code: 'BIND123', expiresAt: '2099-01-01T00:00:00Z' });
app.router.get('/api/runners?projectId=p1', [{ ...runner, name: '测试 机器' }]);
app.router.get('/api/runners/default?projectId=p1', { runnerId: 'runner-mac', personalRunnerId: 'runner-mac', platformRunner });
app.evaluate(`
state.currentUser={id:1,loginName:'owner'};
state.data.users=[{id:1,loginName:'owner',codexApiKeyConfigured:true}];
state.data.projects=[{id:'p1',name:'Shop 项目',ownerId:1,members:[]}];
state.projectFilter='p1';
`);
await app.evaluate('refreshRunners(true)');
await settle(6);
const { document, window } = app;
// Run i18n.js over the rendered page the way its MutationObserver would.
const english = el => el.closest('[data-i18n-data]') ? el.textContent : window.lfTranslate(el.textContent);
const name = document.querySelector('#runnersList .runner-card-name b');
assert.equal(english(name), '测试 机器');
assert.equal(english(document.querySelector('#runnerPoolStatus')), 'Personal default · 测试 机器');
assert.ok(document.querySelector('[data-runner-run-limit]').closest('[data-i18n-data]'), 'the aria-label carries the runner name');
app.evaluate(`window.__share=editRunnerSharing(state.runners[0])`);
await settle(2);
assert.equal(document.querySelector('#confirmTitle').textContent, 'Share 测试 机器');
assert.equal(english(document.querySelector('#confirmTitle span')), '测试 机器');
assert.equal(english(document.querySelector('.runner-share-projects label span')), 'Shop 项目');
document.querySelector('#confirmCancelBtn').click();
await app.evaluate('window.__share');
});

test('English mode translates the runner default toasts the server sends', async (t) => {
const app = await loadApp({ lang: 'en', dispatchReady: false });
t.after(app.close);
for (const message of ['已设为默认执行机器,你的任务和沙箱将在这台机器上执行', '已切回云端执行', '已设为你在此项目的默认执行机器',
'已开启此项目 LOOP 自动分配', '已设为此项目使用平台执行机器', '已清除此项目的默认执行机器',
'自动分配 · 等待可用机器', '自动分配 · 平台云端', '自动分配失败']) {
assert.doesNotMatch(app.window.lfTranslate(message), HAN, message);
}
});

test('Chinese 执行机器 page keeps its Chinese copy', async (t) => {
const app = await runnersPage('', { runnerId: 'runner-mac', personalRunnerId: 'runner-mac' });
t.after(app.close);
const { document } = app;
assert.equal(document.documentElement.dataset.lang, 'zh');
const limit = document.querySelector('.runner-platform-card .runner-run-limit label');
assert.deepEqual([...limit.childNodes].filter(n => n.nodeType === 3).map(n => n.nodeValue), ['最多同时运行', '条']);
assert.equal(document.querySelector('.runner-platform-card .runner-load').textContent, '运行中 0 · 已占 1 / 10');
assert.equal(document.querySelector('#runnerPoolStatus').textContent, '个人默认 · mac-mini');
assert.match(document.querySelector('#runnersList').textContent, /还没有机器|mac-mini/);
assert.equal(app.evaluate(`runnerConcurrencyLabel({runLimit:3})`), '最多同时运行 3 条');
});
Loading