|
16 | 16 | * The editor's canvas is a stub that prints the buffer and patches one field |
17 | 17 | * label, so the page's own load, autosave and publish refreshes are what is |
18 | 18 | * under test. |
| 19 | + * |
| 20 | + * objectstack#30786 — the second describe holds the same for a view, an app and |
| 21 | + * a dashboard, whose served draft is translated the same way: measured on a |
| 22 | + * zh-CN stack, the save refresh installed the served draft (`紧急`, `功能展示`, |
| 23 | + * the dashboard's widget titles) and the next save stored it as source text. |
19 | 24 | */ |
20 | 25 |
|
21 | 26 | import '@testing-library/jest-dom/vitest'; |
@@ -102,7 +107,13 @@ vi.mock('./useMetadata', async (importOriginal) => { |
102 | 107 | ...mod, |
103 | 108 | useMetadataClient: () => mockClient, |
104 | 109 | useMetadataTypes: () => ({ |
105 | | - entries: [{ type: 'object', name: 'object', label: 'Object', allowEnvironmentOverlay: true, schema: { required: [] } }], |
| 110 | + entries: ['object', 'view', 'app', 'dashboard'].map((type) => ({ |
| 111 | + type, |
| 112 | + name: type, |
| 113 | + label: type, |
| 114 | + allowEnvironmentOverlay: true, |
| 115 | + schema: { required: [] }, |
| 116 | + })), |
106 | 117 | }), |
107 | 118 | }; |
108 | 119 | }); |
@@ -239,3 +250,199 @@ describe('MetadataResourceEditPage in a zh-CN console — an object buffer is th |
239 | 250 | expect(srv.saves).toEqual([]); |
240 | 251 | }); |
241 | 252 | }); |
| 253 | + |
| 254 | +// ── objectstack#30786 — a view, an app and a dashboard ────────────────────── |
| 255 | + |
| 256 | +interface TranslatedType { |
| 257 | + type: 'view' | 'app' | 'dashboard'; |
| 258 | + name: string; |
| 259 | + /** The item as its author wrote it (the stored row; source text only). */ |
| 260 | + authored: Row; |
| 261 | + /** The served `?state=draft` body for a zh-CN console, as measured. */ |
| 262 | + zh: (doc: Row) => Row; |
| 263 | + /** One edit, through the page's own patch door. */ |
| 264 | + patch: (draft: Row) => Row; |
| 265 | + /** Every translatable string of `doc`, in source text. */ |
| 266 | + sourceText: (doc: Row) => unknown; |
| 267 | +} |
| 268 | + |
| 269 | +const TRANSLATED_TYPES: TranslatedType[] = [ |
| 270 | + { |
| 271 | + type: 'view', |
| 272 | + name: 'showcase_task.urgent', |
| 273 | + authored: { |
| 274 | + name: 'showcase_task.urgent', |
| 275 | + object: 'showcase_task', |
| 276 | + viewKind: 'list', |
| 277 | + label: 'Urgent', |
| 278 | + config: { label: 'Urgent', type: 'grid', data: { provider: 'object', object: 'showcase_task' }, columns: [{ field: 'title' }] }, |
| 279 | + order: 1, |
| 280 | + }, |
| 281 | + zh: (doc) => ({ ...doc, label: '紧急' }), |
| 282 | + patch: (draft) => ({ order: Number(draft.order ?? 0) + 1 }), |
| 283 | + sourceText: (doc) => doc.label, |
| 284 | + }, |
| 285 | + { |
| 286 | + type: 'app', |
| 287 | + name: 'showcase_app', |
| 288 | + authored: { |
| 289 | + name: 'showcase_app', |
| 290 | + label: 'Showcase', |
| 291 | + navigation: [ |
| 292 | + { id: 'nav_capability_map', type: 'page', label: 'Capability Map', pageName: 'showcase_capability_map' }, |
| 293 | + { id: 'nav_workspace', type: 'group', label: 'Workspace', children: [{ id: 'nav_my_work', type: 'page', label: 'My Work', pageName: 'showcase_my_work' }] }, |
| 294 | + ], |
| 295 | + }, |
| 296 | + zh: (doc) => { |
| 297 | + const nav = doc.navigation as Row[]; |
| 298 | + return { |
| 299 | + ...doc, |
| 300 | + label: '功能展示', |
| 301 | + navigation: [ |
| 302 | + { ...nav[0], label: '能力地图' }, |
| 303 | + { ...nav[1], label: '工作区', children: [{ ...(nav[1].children as Row[])[0], label: '我的工作' }] }, |
| 304 | + ], |
| 305 | + }; |
| 306 | + }, |
| 307 | + patch: (draft) => ({ description: `${String(draft.description ?? '')}edited` }), |
| 308 | + sourceText: (doc) => { |
| 309 | + const nav = doc.navigation as Row[]; |
| 310 | + return [doc.label, nav[0].label, nav[1].label, (nav[1].children as Row[])[0].label]; |
| 311 | + }, |
| 312 | + }, |
| 313 | + { |
| 314 | + type: 'dashboard', |
| 315 | + name: 'showcase_ops_dashboard', |
| 316 | + authored: { |
| 317 | + name: 'showcase_ops_dashboard', |
| 318 | + label: 'Delivery Operations', |
| 319 | + widgets: [ |
| 320 | + { id: 'active_projects', type: 'metric', title: 'Active Projects', layout: { x: 0, y: 0, w: 3, h: 2 } }, |
| 321 | + { id: 'tasks_by_status', type: 'bar', title: 'Tasks by Status', layout: { x: 3, y: 0, w: 6, h: 4 } }, |
| 322 | + ], |
| 323 | + }, |
| 324 | + zh: (doc) => { |
| 325 | + const widgets = doc.widgets as Row[]; |
| 326 | + return { ...doc, label: '交付运营', widgets: [{ ...widgets[0], title: '进行中的项目' }, { ...widgets[1], title: '任务状态分布' }] }; |
| 327 | + }, |
| 328 | + patch: (draft) => ({ description: `${String(draft.description ?? '')}edited` }), |
| 329 | + sourceText: (doc) => { |
| 330 | + const widgets = doc.widgets as Row[]; |
| 331 | + return [doc.label, widgets[0].title, widgets[1].title]; |
| 332 | + }, |
| 333 | + }, |
| 334 | +]; |
| 335 | + |
| 336 | +const SOURCE_TEXT: Record<TranslatedType['type'], unknown> = { |
| 337 | + view: 'Urgent', |
| 338 | + app: ['Showcase', 'Capability Map', 'Workspace', 'My Work'], |
| 339 | + dashboard: ['Delivery Operations', 'Active Projects', 'Tasks by Status'], |
| 340 | +}; |
| 341 | + |
| 342 | +/** Canvas stand-in for one type: prints the buffer, and makes the type's one edit. */ |
| 343 | +function stubCanvasFor(spec: TranslatedType) { |
| 344 | + return function TypeStubCanvas({ draft, onPatch }: { draft: Row; onPatch?: (patch: Row) => void }) { |
| 345 | + return ( |
| 346 | + <> |
| 347 | + <pre data-testid="editor-draft">{JSON.stringify(draft)}</pre> |
| 348 | + <button type="button" onClick={() => onPatch?.(spec.patch(draft))}> |
| 349 | + edit once |
| 350 | + </button> |
| 351 | + </> |
| 352 | + ); |
| 353 | + }; |
| 354 | +} |
| 355 | + |
| 356 | +function renderTypeZh(spec: TranslatedType) { |
| 357 | + const path = `/metadata/${spec.type}/${spec.name}`; |
| 358 | + window.history.replaceState(null, '', path); |
| 359 | + render( |
| 360 | + <I18nProvider config={{ defaultLanguage: 'zh', detectBrowserLanguage: false }} persistLanguage={false}> |
| 361 | + <MemoryRouter initialEntries={[path]}> |
| 362 | + <MetadataResourceEditPage type={spec.type} name={spec.name} /> |
| 363 | + </MemoryRouter> |
| 364 | + </I18nProvider>, |
| 365 | + ); |
| 366 | +} |
| 367 | + |
| 368 | +describe.each(TRANSLATED_TYPES)( |
| 369 | + 'MetadataResourceEditPage in a zh-CN console — a $type buffer is the authored document (objectstack#30786)', |
| 370 | + (spec) => { |
| 371 | + const realTypePreview = getMetadataPreview(spec.type); |
| 372 | + |
| 373 | + beforeEach(() => { |
| 374 | + srv.overlay = null; |
| 375 | + srv.draft = null; |
| 376 | + srv.servesDraftLayer = true; |
| 377 | + srv.saves.length = 0; |
| 378 | + registerMetadataPreview(spec.type, stubCanvasFor(spec) as never); |
| 379 | + // A shipped item: the stored row is the package's (`code`), and the |
| 380 | + // layered read serves every layer untranslated. |
| 381 | + mockClient.layered.mockImplementation(async () => ({ |
| 382 | + code: wire(spec.authored), |
| 383 | + overlay: srv.overlay, |
| 384 | + overlayScope: srv.overlay ? 'env' : null, |
| 385 | + effective: wire(srv.overlay ?? spec.authored), |
| 386 | + ...(srv.servesDraftLayer ? { draft: srv.draft } : {}), |
| 387 | + editable: true, |
| 388 | + deletable: true, |
| 389 | + resettable: true, |
| 390 | + lock: 'none', |
| 391 | + })); |
| 392 | + mockClient.getDraft.mockImplementation(async () => { |
| 393 | + if (!srv.draft) { |
| 394 | + throw Object.assign(new Error(`No pending draft exists for ${spec.type}/${spec.name}.`), { code: 'NO_DRAFT', status: 404 }); |
| 395 | + } |
| 396 | + return { type: spec.type, name: spec.name, item: { ...spec.zh(srv.draft), _diagnostics: { valid: true } } }; |
| 397 | + }); |
| 398 | + }); |
| 399 | + |
| 400 | + afterEach(() => { |
| 401 | + if (realTypePreview) registerMetadataPreview(spec.type, realTypePreview); |
| 402 | + }); |
| 403 | + |
| 404 | + it('no pending draft: two edits in a row each PUT source text, the second after the save refresh re-read the draft', async () => { |
| 405 | + renderTypeZh(spec); |
| 406 | + await waitFor(() => expect(editorDraft().name).toBe(spec.name), { timeout: 8000 }); |
| 407 | + expect(spec.sourceText(editorDraft())).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 408 | + |
| 409 | + fireEvent.click(screen.getByRole('button', { name: 'edit once' })); |
| 410 | + await waitFor(() => expect(srv.saves.length).toBe(1), { timeout: 8000 }); |
| 411 | + expect(srv.saves[0]!.opts).toMatchObject({ mode: 'draft' }); |
| 412 | + expect(spec.sourceText(srv.saves[0]!.body)).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 413 | + |
| 414 | + // The save refresh: the served draft is now the zh-CN one, the layers' the stored one. |
| 415 | + await waitFor(() => expect(mockClient.getDraft.mock.calls.length).toBeGreaterThan(1), { timeout: 8000 }); |
| 416 | + await act(async () => { |
| 417 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 418 | + }); |
| 419 | + expect(spec.sourceText(editorDraft())).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 420 | + |
| 421 | + fireEvent.click(screen.getByRole('button', { name: 'edit once' })); |
| 422 | + await waitFor(() => expect(srv.saves.length).toBe(2), { timeout: 8000 }); |
| 423 | + expect(spec.sourceText(srv.saves[1]!.body)).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 424 | + expect(spec.sourceText(srv.draft!)).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 425 | + }); |
| 426 | + |
| 427 | + it('a pending draft: the buffer is the stored draft, and one edit PUTs it in source text', async () => { |
| 428 | + srv.draft = wire({ ...spec.authored, ...spec.patch(spec.authored) }); |
| 429 | + renderTypeZh(spec); |
| 430 | + await waitFor(() => expect(editorDraft().name).toBe(spec.name), { timeout: 8000 }); |
| 431 | + expect(spec.sourceText(editorDraft())).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 432 | + |
| 433 | + fireEvent.click(screen.getByRole('button', { name: 'edit once' })); |
| 434 | + await waitFor(() => expect(srv.saves.length).toBeGreaterThan(0), { timeout: 8000 }); |
| 435 | + expect(spec.sourceText(srv.saves.at(-1)!.body)).toStrictEqual(SOURCE_TEXT[spec.type]); |
| 436 | + }); |
| 437 | + |
| 438 | + it('a served draft whose stored copy the layers do not carry installs no buffer, and nothing is sent', async () => { |
| 439 | + srv.draft = wire(spec.authored); |
| 440 | + srv.servesDraftLayer = false; |
| 441 | + renderTypeZh(spec); |
| 442 | + |
| 443 | + await screen.findByText(t('engine.studio.refusal.draftUnreadable', 'zh-CN'), undefined, { timeout: 8000 }); |
| 444 | + expect(screen.queryByTestId('editor-draft')).toBeNull(); |
| 445 | + expect(srv.saves).toEqual([]); |
| 446 | + }); |
| 447 | + }, |
| 448 | +); |
0 commit comments