You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/UserCopyBrowserRuntimeTest.mjs

56 lines
2.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import vm from 'node:vm';
const root = new URL('../', import.meta.url);
const source = await readFile(new URL('public/cloud/user-copy-runtime.js', root), 'utf8');
const payload = {
schema: 1,
locale: 'en',
messages: {
'common.toast.success.title': '运行时成功',
'common.toast.close.aria': '运行时关闭',
'test.greeting': '您好 {name}',
'test.count': '{count, plural, =0 {暂无项目} one {# 个项目} other {# 个项目}}',
'test.role': '{role, select, owner {所有者} other {成员}}',
},
metadata: {
'test.greeting': { format: 'icu', parameters: ['name'] },
'test.count': { format: 'icu', parameters: ['count'] },
'test.role': { format: 'icu', parameters: ['role'] },
},
};
const context = {
Intl, Date, JSON,
document: {
getElementById(id) {
return id === 'user-copy-bootstrap'
? { textContent: JSON.stringify(payload) } : null;
},
},
};
context.globalThis = context;
vm.runInNewContext(source, context, { filename: 'user-copy-runtime.js' });
const runtime = context.VenixUserCopy;
assert.equal(runtime.locale, 'en');
assert.equal(runtime.text('common.toast.success.title'), '运行时成功');
assert.equal(runtime.text('test.greeting', { name: 'Virt' }), '您好 Virt');
assert.equal(runtime.text('test.count', { count: 0 }), '暂无项目');
assert.equal(runtime.text('test.count', { count: 2 }), '2 个项目');
assert.equal(runtime.text('test.role', { role: 'owner' }), '所有者');
assert.equal(runtime.text('missing.copy.key'), 'missing.copy.key');
assert.match(runtime.number(1234), /1,234/);
assert.match(runtime.bytes(1024), /1 KiB/);
const toast = await readFile(new URL('public/cloud/toast.js', root), 'utf8');
assert.match(toast, /VenixUserCopy/);
assert.match(toast, /i18n\.text/,
'Cloud Toast does not read defaults from the browser catalog.');
assert.doesNotMatch(toast, /const copy = globalThis\.VenixUserCopy/,
'The catalog binding must not be shadowed by the Toast copy container.');
assert.doesNotMatch(toast, /操作失败|操作完成|关闭提示/,
'Cloud Toast still owns source-language defaults.');
console.log('User copy browser translator and Toast defaults passed.');