|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Success-envelope conformance for the dispatcher's `/i18n` domain — the |
| 5 | + * missing twin of `service-i18n`'s `error-envelope.conformance.test.ts`, and |
| 6 | + * the same pairing storage got in #3689. |
| 7 | + * |
| 8 | + * WHY THIS SUITE EXISTS |
| 9 | + * |
| 10 | + * Three consecutive defects landed on this one route family, and every one of |
| 11 | + * them was a body that did not match the schema declaring it: |
| 12 | + * |
| 13 | + * #3676 the request declared `namespace`/`keys` filters no server read |
| 14 | + * #3833 the field-labels derivation scanned a retired key dialect and so |
| 15 | + * returned `{}` for every provider |
| 16 | + * #3847 `labels` entries were emitted as bare strings under a schema |
| 17 | + * declaring `{ label, help?, options? }` |
| 18 | + * |
| 19 | + * All three survived a green test suite, because every test asserted the |
| 20 | + * emitted body against a HAND-WRITTEN LITERAL. Comparing output to a literal |
| 21 | + * proves the code does what the test author believed; it cannot prove the code |
| 22 | + * does what the contract declares. Nothing had ever put the emitted value and |
| 23 | + * the declared schema in the same assertion. |
| 24 | + * |
| 25 | + * So these assertions parse each response with the schema `plugin-rest-api` |
| 26 | + * names for that route (`responseSchema: 'GetLocalesResponseSchema'`, …), |
| 27 | + * imported rather than restated. A body that parses is a body the SDK's |
| 28 | + * published return type does not lie about. |
| 29 | + * |
| 30 | + * The first thing the suite caught, on the day it was written: `GET |
| 31 | + * /i18n/locales` passed `getLocales()`'s raw `string[]` straight through, |
| 32 | + * while `GetLocalesResponseSchema` declares `{ code, label, isDefault }[]` and |
| 33 | + * service-i18n — the OTHER provider of this identical route — already emitted |
| 34 | + * descriptors. One endpoint, two shapes, decided by which plugin mounted it. |
| 35 | + * The dispatcher's copy of the field-labels derivation went wrong the same way |
| 36 | + * in #3833, one route over, which is why both mappings are now shared. |
| 37 | + * |
| 38 | + * The route ledgers cannot catch this. They audit which routes exist and |
| 39 | + * whether the SDK can address them, not what comes back. |
| 40 | + */ |
| 41 | + |
| 42 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 43 | +import { HttpDispatcher } from './http-dispatcher.js'; |
| 44 | +import { BaseResponseSchema } from '@objectstack/spec/api'; |
| 45 | +import { |
| 46 | + GetLocalesResponseSchema, |
| 47 | + GetTranslationsResponseSchema, |
| 48 | + GetFieldLabelsResponseSchema, |
| 49 | +} from '@objectstack/spec/api'; |
| 50 | + |
| 51 | +/** The nested shape every producer writes (#3778). */ |
| 52 | +const BUNDLE = { |
| 53 | + objects: { |
| 54 | + contact: { |
| 55 | + label: '联系人', |
| 56 | + fields: { |
| 57 | + first_name: { label: '名' }, |
| 58 | + email: { label: '邮箱', help: '主邮箱地址' }, |
| 59 | + status: { label: '状态', options: { open: '进行中', closed: '已关闭' } }, |
| 60 | + phone: { help: '优先手机' }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + messages: { save: '保存' }, |
| 65 | +}; |
| 66 | + |
| 67 | +describe('/i18n success-envelope conformance (dispatcher domain)', () => { |
| 68 | + let dispatcher: HttpDispatcher; |
| 69 | + let i18nService: Record<string, unknown>; |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + i18nService = { |
| 73 | + getLocales: vi.fn().mockReturnValue(['en', 'zh-CN']), |
| 74 | + getDefaultLocale: vi.fn().mockReturnValue('en'), |
| 75 | + getTranslations: vi.fn().mockReturnValue(BUNDLE), |
| 76 | + }; |
| 77 | + const kernel = { |
| 78 | + getService: vi.fn(async (name: string) => (name === 'i18n' ? i18nService : null)), |
| 79 | + services: new Map(), |
| 80 | + context: { getService: (name: string) => (name === 'i18n' ? i18nService : null) }, |
| 81 | + }; |
| 82 | + dispatcher = new HttpDispatcher(kernel as never); |
| 83 | + }); |
| 84 | + |
| 85 | + /** Every success body must satisfy the shared envelope. */ |
| 86 | + function expectEnvelope(body: unknown) { |
| 87 | + const parsed = BaseResponseSchema.safeParse(body); |
| 88 | + expect(parsed.success, `body is not a BaseResponse: ${JSON.stringify(body)}`).toBe(true); |
| 89 | + expect((body as { success?: boolean }).success).toBe(true); |
| 90 | + } |
| 91 | + |
| 92 | + it('GET /locales — body satisfies GetLocalesResponseSchema', async () => { |
| 93 | + const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} } as never); |
| 94 | + expect(result.response?.status).toBe(200); |
| 95 | + expectEnvelope(result.response?.body); |
| 96 | + |
| 97 | + const parsed = GetLocalesResponseSchema.safeParse(result.response?.body?.data); |
| 98 | + expect( |
| 99 | + parsed.success, |
| 100 | + `locales body does not match its declared schema: ${JSON.stringify(parsed.error?.issues)}`, |
| 101 | + ).toBe(true); |
| 102 | + expect(parsed.data?.locales).toEqual([ |
| 103 | + { code: 'en', label: 'en', isDefault: true }, |
| 104 | + { code: 'zh-CN', label: 'zh-CN', isDefault: false }, |
| 105 | + ]); |
| 106 | + }); |
| 107 | + |
| 108 | + it('GET /locales — a bare string[] is DEAD, so a revert cannot pass quietly', async () => { |
| 109 | + const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} } as never); |
| 110 | + const locales = result.response?.body?.data?.locales as unknown[]; |
| 111 | + expect(locales.every((l) => typeof l === 'object' && l !== null)).toBe(true); |
| 112 | + expect(locales).not.toContain('en'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('GET /translations/:locale — body satisfies GetTranslationsResponseSchema', async () => { |
| 116 | + const result = await dispatcher.handleI18n('/translations/zh-CN', 'GET', {}, { request: {} } as never); |
| 117 | + expect(result.response?.status).toBe(200); |
| 118 | + expectEnvelope(result.response?.body); |
| 119 | + |
| 120 | + const parsed = GetTranslationsResponseSchema.safeParse(result.response?.body?.data); |
| 121 | + expect( |
| 122 | + parsed.success, |
| 123 | + `translations body does not match its declared schema: ${JSON.stringify(parsed.error?.issues)}`, |
| 124 | + ).toBe(true); |
| 125 | + }); |
| 126 | + |
| 127 | + it('GET /labels/:object/:locale — body satisfies GetFieldLabelsResponseSchema', async () => { |
| 128 | + const result = await dispatcher.handleI18n('/labels/contact/zh-CN', 'GET', {}, { request: {} } as never); |
| 129 | + expect(result.response?.status).toBe(200); |
| 130 | + expectEnvelope(result.response?.body); |
| 131 | + |
| 132 | + const parsed = GetFieldLabelsResponseSchema.safeParse(result.response?.body?.data); |
| 133 | + expect( |
| 134 | + parsed.success, |
| 135 | + `labels body does not match its declared schema: ${JSON.stringify(parsed.error?.issues)}`, |
| 136 | + ).toBe(true); |
| 137 | + // The entries are objects carrying what the bundle holds — the gap #3847 |
| 138 | + // closed, pinned here against the contract rather than a literal. |
| 139 | + expect(parsed.data?.labels.email?.help).toBe('主邮箱地址'); |
| 140 | + expect(parsed.data?.labels.status?.options?.open).toBe('进行中'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('an empty label map is still a conforming body', async () => { |
| 144 | + const result = await dispatcher.handleI18n('/labels/unknown_object/zh-CN', 'GET', {}, { request: {} } as never); |
| 145 | + expect(result.response?.status).toBe(200); |
| 146 | + expect(GetFieldLabelsResponseSchema.safeParse(result.response?.body?.data).success).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + /** |
| 150 | + * A provider may omit the optional `getDefaultLocale`. The descriptor |
| 151 | + * mapping must still produce a conforming body — `isDefault` is required by |
| 152 | + * the schema, so an undefined default must yield `false`, not `undefined`. |
| 153 | + */ |
| 154 | + it('GET /locales conforms even when the provider omits getDefaultLocale', async () => { |
| 155 | + delete i18nService.getDefaultLocale; |
| 156 | + const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} } as never); |
| 157 | + const parsed = GetLocalesResponseSchema.safeParse(result.response?.body?.data); |
| 158 | + expect(parsed.success).toBe(true); |
| 159 | + expect(parsed.data?.locales.every((l) => l.isDefault === false)).toBe(true); |
| 160 | + }); |
| 161 | +}); |
0 commit comments