|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// objectstack#26170 — a hook's declared refusal catalogue joins the i18n |
| 4 | +// buckets as `hooks.<hook>.refusals.<CODE>.message` (maintainer ruling |
| 5 | +// 6431311681, letter A). |
| 6 | +// |
| 7 | +// A hook declaring `refusals: { <CODE>: { message, status? } }` refuses by code |
| 8 | +// only, and the sandboxed body runner reads the key in the caller's locale |
| 9 | +// before it answers. These pins are the CLI half: the walker scaffolds one key |
| 10 | +// per declared entry, the coverage gate demands it, and the key the walker |
| 11 | +// writes is the very address the runtime asks for (`hookRefusalMessageKey`), |
| 12 | +// so the two cannot drift. |
| 13 | +// |
| 14 | +// The ledger mock is `i18n-flow-refusal-coverage.test.ts`'s, for its reason: |
| 15 | +// these pins measure the bucket itself, whatever the shipped ledger says next. |
| 16 | + |
| 17 | +import { describe, it, expect, vi } from 'vitest'; |
| 18 | + |
| 19 | +vi.mock('@objectstack/lint', async (importOriginal) => ({ |
| 20 | + ...(await importOriginal<typeof import('@objectstack/lint')>()), |
| 21 | + authorWarnedProperties: () => new Set<string>(), |
| 22 | +})); |
| 23 | + |
| 24 | +import { collectExpectedEntries, extractTranslations } from '../src/utils/i18n-extract.js'; |
| 25 | +import { computeI18nCoverage } from '../src/utils/i18n-coverage.js'; |
| 26 | +import { TranslationDataSchema, hookRefusalMessageKey } from '@objectstack/spec/system'; |
| 27 | + |
| 28 | +const LIMIT = 'This order exceeds the customer credit limit.'; |
| 29 | +const HOLD = 'The account is on hold.'; |
| 30 | + |
| 31 | +const creditGuard = { |
| 32 | + name: 'credit_guard', |
| 33 | + object: 'order', |
| 34 | + events: ['beforeInsert'], |
| 35 | + body: { language: 'js', source: "throw Object.assign(new Error('x'), { code: 'CREDIT_LIMIT' });" }, |
| 36 | + refusals: { |
| 37 | + CREDIT_LIMIT: { message: LIMIT }, |
| 38 | + ACCOUNT_ON_HOLD: { message: HOLD, status: 409 }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +/** A hook with no catalogue contributes nothing. */ |
| 43 | +const plainHook = { name: 'stamp', object: 'order', events: ['beforeInsert'], handler: 'stamp' }; |
| 44 | + |
| 45 | +const hookEntries = (config: any) => collectExpectedEntries(config).filter((e) => e.path[0] === 'hooks'); |
| 46 | + |
| 47 | +describe('what the walker harvests from a hook catalogue (#26170)', () => { |
| 48 | + it('emits one key per declared entry, keyed as the runtime reads it, seeded with the message', () => { |
| 49 | + const entries = hookEntries({ hooks: [creditGuard, plainHook] }); |
| 50 | + |
| 51 | + expect(entries.map((e) => e.path.join('.'))).toEqual([ |
| 52 | + hookRefusalMessageKey('credit_guard', 'CREDIT_LIMIT'), |
| 53 | + hookRefusalMessageKey('credit_guard', 'ACCOUNT_ON_HOLD'), |
| 54 | + ]); |
| 55 | + expect(entries[0]).toMatchObject({ sourceValue: LIMIT, inline: LIMIT, source: 'hook' }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('harvests nothing from a stack whose hooks declare no catalogue', () => { |
| 59 | + expect(hookEntries({ hooks: [plainHook] })).toEqual([]); |
| 60 | + expect(hookEntries({})).toEqual([]); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('the coverage gate demands each declared refusal (#26170)', () => { |
| 65 | + const tree = (hooks?: Record<string, unknown>) => ({ |
| 66 | + i18n: { defaultLocale: 'en', supportedLocales: ['en', 'zh-CN'] }, |
| 67 | + hooks: [creditGuard], |
| 68 | + translations: [{ 'zh-CN': { ...(hooks ? { hooks } : {}) } }], |
| 69 | + }); |
| 70 | + const zhHooks = (config: any) => |
| 71 | + computeI18nCoverage(config).issues.filter((i) => i.locale === 'zh-CN' && i.key.startsWith('hooks.')); |
| 72 | + |
| 73 | + it('reports each untranslated refusal in the hook bucket', () => { |
| 74 | + const issues = zhHooks(tree()); |
| 75 | + expect(issues.map((i) => i.key).sort()).toEqual([ |
| 76 | + 'hooks.credit_guard.refusals.ACCOUNT_ON_HOLD.message', |
| 77 | + 'hooks.credit_guard.refusals.CREDIT_LIMIT.message', |
| 78 | + ]); |
| 79 | + expect(issues.every((i) => i.source === 'hook')).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it('counts what is still missing once one is translated', () => { |
| 83 | + const issues = zhHooks(tree({ credit_guard: { refusals: { CREDIT_LIMIT: { message: '订单超出客户信用额度。' } } } })); |
| 84 | + expect(issues.map((i) => i.key)).toEqual(['hooks.credit_guard.refusals.ACCOUNT_ON_HOLD.message']); |
| 85 | + }); |
| 86 | + |
| 87 | + it('goes quiet once every entry is translated', () => { |
| 88 | + const issues = zhHooks(tree({ |
| 89 | + credit_guard: { |
| 90 | + refusals: { |
| 91 | + CREDIT_LIMIT: { message: '订单超出客户信用额度。' }, |
| 92 | + ACCOUNT_ON_HOLD: { message: '该客户账户已冻结。' }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + })); |
| 96 | + expect(issues).toEqual([]); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('the skeleton `os i18n extract` writes (#26170)', () => { |
| 101 | + const result = extractTranslations({ hooks: [creditGuard] }, { locales: ['en', 'zh-CN'] }); |
| 102 | + |
| 103 | + it('writes the message into the default locale and an empty slot into the others', () => { |
| 104 | + expect((result.bundles.en as any).hooks.credit_guard.refusals).toEqual({ |
| 105 | + CREDIT_LIMIT: { message: LIMIT }, |
| 106 | + ACCOUNT_ON_HOLD: { message: HOLD }, |
| 107 | + }); |
| 108 | + expect((result.bundles['zh-CN'] as any).hooks.credit_guard.refusals).toEqual({ |
| 109 | + CREDIT_LIMIT: { message: '' }, |
| 110 | + ACCOUNT_ON_HOLD: { message: '' }, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('parses through the schema it is written for, in every locale', () => { |
| 115 | + for (const [locale, bundle] of Object.entries(result.bundles)) { |
| 116 | + expect(TranslationDataSchema.safeParse(bundle).success, `skeleton for ${locale}`).toBe(true); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
0 commit comments