Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(i18n): 修新增菜单测试的 exempt 前缀与 proposed 处理,pattern 异常转可读错误

Dashbbc2469ad806
diff --git a/apps/desktop/src/main/__tests__/applicationMenuLabels.test.ts b/apps/desktop/src/main/__tests__/applicationMenuLabels.test.ts
index bed52aef05..0ae938848a 100644
--- a/apps/desktop/src/main/__tests__/applicationMenuLabels.test.ts
+++ b/apps/desktop/src/main/__tests__/applicationMenuLabels.test.ts
@@ -47,11 +47,18 @@ const glossary = JSON.parse(
readFileSync(resolve(REPO_ROOT, 'i18n/glossary.json'), 'utf8'),
) as { locales: string[]; sourceLocale: string; terms: GlossaryTerm[] };
-/** 摊平成 (locale, key, value)。key 形态与根门禁一致,便于 exempt 复用同一套写法。 */
+/**
+ * 摊平成 (locale, key, value)。
+ *
+ * 前缀必须是 `desktop:`——glossary.json 的 exempt 用 `^(desktop|mobile/xxx):` 校验格式,
+ * 也按这个前缀匹配。用 `desktop-menu:` 之类的自造前缀会让 exempt 对这份 catalog 完全
+ * 失效(而 note 里还写着"可复用同一套写法"),将来给某个菜单项加豁免时会白写一条。
+ * 用 `menu.` 子命名空间区分来源。
+ */
const entries = Object.entries(APPLICATION_MENU_LABELS).flatMap(([locale, labels]) =>
Object.entries(labels).map(([key, value]) => ({
locale,
- key: `desktop-menu:${key}`,
+ key: `desktop:menu.${key}`,
value: value as string,
})),
);
@@ -101,17 +108,24 @@ describe('原生应用菜单标签符合术语表', () => {
it('保留英文的术语大小写形态统一', () => {
const violations: string[] = [];
+ // 与 forbidden 用例同一分级:proposed 也扫,只是降级为告警。跳过的话,菜单文案里
+ // 「待裁决术语当前命中多少处」的数据就没了,而根门禁是会统计的。
+ const notes: string[] = [];
for (const term of glossary.terms) {
- if (term.status !== 'decided' || term.checkCase === false) continue;
+ if (term.checkCase === false) continue;
const isExempt = makeExemptChecker(term.exempt);
for (const { locale, key, value } of entries) {
if (isExempt(key)) continue;
const standard = term.translations?.[locale];
if (!standard || standard !== term.en) continue;
const hit = findCaseMismatch(stripNonProse(value), standard);
- if (hit) violations.push(`${locale} ${key}: 「${hit}」应为「${standard}」`);
+ if (!hit) continue;
+ const line = `${locale} ${key}: 「${hit}」应为「${standard}」`;
+ if (term.status === 'decided') violations.push(line);
+ else notes.push(line);
}
}
+ if (notes.length > 0) console.warn(`[menu-glossary] 待裁决术语大小写:\n${notes.join('\n')}`);
expect(violations, `原生菜单大小写不统一:\n${violations.join('\n')}`).toEqual([]);
});
diff --git a/scripts/__tests__/glossary-rules.test.mjs b/scripts/__tests__/glossary-rules.test.mjs
index 0eedd9e690..0e4a1ebf50 100644
--- a/scripts/__tests__/glossary-rules.test.mjs
+++ b/scripts/__tests__/glossary-rules.test.mjs
@@ -531,3 +531,12 @@ test('GLOSSARY.md 必须声明「参考,不是替换表」', () => {
assert.ok(doc.includes('不是替换表'), 'GLOSSARY.md 缺少定位声明');
assert.ok(doc.includes('禁止拿本表做脚本批量替换'), 'GLOSSARY.md 缺少批量替换禁令');
});
+
+test('validateAgainstSchema: schema 的 pattern 写坏时报可读错误而不是抛栈', () => {
+ // 直接 new RegExp 会把异常抛出校验流程,调用方拿不到带 path 的错误、
+ // check-i18n-glossary 会崩栈而不是走 fail()
+ const schema = { type: 'object', properties: { id: { type: 'string', pattern: '[' } } };
+ const errors = validateAgainstSchema({ id: 'x' }, schema);
+ assert.equal(errors.length, 1);
+ assert.match(errors[0], /\$\.id: schema 的 pattern 不是合法正则/);
+});
diff --git a/scripts/shared/json-schema-lite.mjs b/scripts/shared/json-schema-lite.mjs
index 3f47bde820..799a0f9805 100644
--- a/scripts/shared/json-schema-lite.mjs
+++ b/scripts/shared/json-schema-lite.mjs
@@ -103,8 +103,20 @@ function validateNode(value, schema, root, path, errors) {
}
if (typeof value === 'string') {
- if (schema.pattern && !new RegExp(schema.pattern).test(value)) {
- errors.push(`${path}: "${value}" 不匹配 ${schema.pattern}`);
+ if (schema.pattern) {
+ // schema 里的 pattern 写坏时,new RegExp 会抛出去、整个校验流程中断,调用方拿不到
+ // 带 path 的可读错误(check-i18n-glossary 会直接崩栈而不是走 fail())。
+ // 转成普通校验错误,并明确指向是 schema 自身的问题而非数据的问题。
+ let re;
+ try {
+ re = new RegExp(schema.pattern);
+ } catch (err) {
+ errors.push(`${path}: schema 的 pattern 不是合法正则(${schema.pattern}):${err.message}`);
+ re = null;
+ }
+ if (re && !re.test(value)) {
+ errors.push(`${path}: "${value}" 不匹配 ${schema.pattern}`);
+ }
}
if (schema.minLength !== undefined && value.length < schema.minLength) {
errors.push(`${path}: 长度需 ≥ ${schema.minLength}`);