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; }, querySelector() { return 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 ordinaryNode = { attributes: {}, setAttribute(name, value) { this.attributes[name] = value; }, }; assert.equal(runtime.bind(ordinaryNode, 'test.greeting'), ordinaryNode); assert.deepEqual(ordinaryNode.attributes, {}, 'Ordinary users received a dynamic authoring marker.'); const authorContext = { Intl, Date, JSON, document: { getElementById(id) { return id === 'user-copy-bootstrap' ? { textContent: JSON.stringify(payload) } : null; }, querySelector(selector) { return selector === '[data-user-copy-key]' ? {} : null; }, }, }; authorContext.globalThis = authorContext; vm.runInNewContext(source, authorContext, { filename: 'user-copy-runtime.js' }); const authorNode = { attributes: {}, setAttribute(name, value) { this.attributes[name] = value; }, }; assert.equal(authorContext.VenixUserCopy.bind(authorNode, 'test.greeting'), authorNode); assert.equal(authorNode.attributes['data-user-copy-key'], 'test.greeting'); assert.throws( () => authorContext.VenixUserCopy.bind(authorNode, 'missing.copy.key'), /Unknown user copy binding key/, ); assert.throws( () => authorContext.VenixUserCopy.bind({}, 'test.greeting'), /binding target must be an element/, ); 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, dynamic bindings and Toast defaults passed.');