|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; |
21 | | -import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 21 | +import { render, screen, cleanup, fireEvent, waitFor, within } from '@testing-library/react'; |
22 | 22 | import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; |
23 | 23 |
|
24 | 24 | // ── Sample metadata the mocked adapter serves (two packages, no books) ────── |
@@ -72,17 +72,48 @@ const { ADAPTER, WORLD } = vi.hoisted(() => { |
72 | 72 | acme_intro: 'ACME BODY', |
73 | 73 | ops_setup: 'OPS BODY', |
74 | 74 | }; |
75 | | - const WORLD = { authored: false }; |
| 75 | + // objectstack#34070 — a third world, switched on only by the "two packages |
| 76 | + // ship one doc name" block: `shared_guide` is shipped by alpha AND beta (beta |
| 77 | + // listed first), and a read of it that names no package is refused the way |
| 78 | + // the server refuses it, `409 RESOURCE_CONFLICT` naming both holders. |
| 79 | + const CONFLICT_DOCS = [ |
| 80 | + { name: 'shared_guide', label: 'Shared Guide', _packageId: 'com.example.beta' }, |
| 81 | + { name: 'beta_start', label: 'Beta Start', _packageId: 'com.example.beta' }, |
| 82 | + { name: 'shared_guide', label: 'Shared Guide', _packageId: 'com.example.alpha' }, |
| 83 | + { name: 'alpha_welcome', label: 'Alpha Welcome', _packageId: 'com.example.alpha' }, |
| 84 | + ]; |
| 85 | + const CONFLICT_CONTENT: Record<string, Record<string, string>> = { |
| 86 | + 'com.example.alpha': { shared_guide: 'ALPHA SHARED BODY', alpha_welcome: 'ALPHA BODY' }, |
| 87 | + 'com.example.beta': { shared_guide: 'BETA SHARED BODY', beta_start: 'BETA BODY' }, |
| 88 | + }; |
| 89 | + /** A rejection shaped like `@objectstack/client`'s on a non-2xx response. */ |
| 90 | + const failure = (httpStatus: number, code: string, message: string, details?: unknown) => |
| 91 | + Object.assign(new Error(message), { httpStatus, code, details }); |
| 92 | + const conflictRead = (name: string, packageId: string | undefined) => { |
| 93 | + if (name === 'broken_doc') throw failure(500, 'INTERNAL_ERROR', 'The metadata service is unavailable'); |
| 94 | + const holders = Object.keys(CONFLICT_CONTENT).filter((p) => name in CONFLICT_CONTENT[p]); |
| 95 | + if (packageId) return { item: { name, content: CONFLICT_CONTENT[packageId]?.[name] } }; |
| 96 | + if (holders.length > 1) { |
| 97 | + throw failure(409, 'RESOURCE_CONFLICT', `doc "${name}" is held by ${holders.length} packages`, { |
| 98 | + candidates: holders.map((p) => ({ kind: 'package', packageId: p })), |
| 99 | + }); |
| 100 | + } |
| 101 | + return { item: { name, content: holders.length ? CONFLICT_CONTENT[holders[0]][name] : undefined } }; |
| 102 | + }; |
| 103 | + const WORLD = { authored: false, conflict: false }; |
76 | 104 | const ADAPTER = { |
77 | 105 | getClient: () => ({ |
78 | 106 | meta: { |
79 | 107 | getItems: async (type: string) => |
80 | | - WORLD.authored |
| 108 | + WORLD.conflict |
| 109 | + ? (type === 'doc' ? CONFLICT_DOCS : []) // implicit books only |
| 110 | + : WORLD.authored |
81 | 111 | ? (type === 'doc' ? AUTHORED_DOCS : type === 'book' ? AUTHORED_BOOKS : []) |
82 | 112 | : (type === 'doc' ? DOCS : []), // no authored books |
83 | | - getItem: async (_type: string, name: string) => ({ |
84 | | - item: { name, content: (WORLD.authored ? AUTHORED_CONTENT : CONTENT)[name] }, |
85 | | - }), |
| 113 | + getItem: async (_type: string, name: string, options?: { packageId?: string }) => |
| 114 | + WORLD.conflict |
| 115 | + ? conflictRead(name, options?.packageId) |
| 116 | + : { item: { name, content: (WORLD.authored ? AUTHORED_CONTENT : CONTENT)[name] } }, |
86 | 117 | }, |
87 | 118 | }), |
88 | 119 | }; |
@@ -436,3 +467,96 @@ describe('objectstack#33055 — the #section survives the flat → canonical red |
436 | 467 | await waitFor(() => expect(scrolled).toEqual(['lead-scoring'])); |
437 | 468 | }); |
438 | 469 | }); |
| 470 | + |
| 471 | + |
| 472 | +// ───────────────────────────────────────────────────────────────────────────── |
| 473 | +// objectstack#34070 — a doc name two packages ship: the reader picks the package |
| 474 | +// ───────────────────────────────────────────────────────────────────────────── |
| 475 | + |
| 476 | +/** |
| 477 | + * Two enabled packages may ship one doc name. A read of it that names no |
| 478 | + * package is refused, `409 RESOURCE_CONFLICT`, with the holders in the |
| 479 | + * refusal's `details.candidates` (ADR-0150 D3; the `/meta` 409 section of |
| 480 | + * `content/docs/api/client-sdk.mdx`). The portal lists those candidates, each |
| 481 | + * linking to its package address `/apps/PACKAGE/docs/NAME`, where the read names |
| 482 | + * the package and the doc opens in that package's own book. One holder, and |
| 483 | + * any other failure, answer as they did. |
| 484 | + */ |
| 485 | +describe('objectstack#34070 — a doc name two packages ship lists its candidate packages', () => { |
| 486 | + function Where() { |
| 487 | + return <div data-testid="where">{useLocation().pathname}</div>; |
| 488 | + } |
| 489 | + function Portal({ entry }: { entry: string }) { |
| 490 | + const children = ( |
| 491 | + <> |
| 492 | + <Route path=":slug" element={<DocsSlug />} /> |
| 493 | + <Route path=":slug/:name" element={<DocPage />} /> |
| 494 | + </> |
| 495 | + ); |
| 496 | + return ( |
| 497 | + <MemoryRouter initialEntries={[entry]}> |
| 498 | + <Where /> |
| 499 | + <Routes> |
| 500 | + <Route path="/docs" element={<DocsLayout />}> |
| 501 | + <Route index element={<DocsIndex />} /> |
| 502 | + {children} |
| 503 | + </Route> |
| 504 | + <Route path="/apps/:appName/docs" element={<DocsLayout />}> |
| 505 | + {children} |
| 506 | + </Route> |
| 507 | + </Routes> |
| 508 | + </MemoryRouter> |
| 509 | + ); |
| 510 | + } |
| 511 | + const candidateList = () => screen.findByRole('list', { name: 'Packages that provide this document' }); |
| 512 | + |
| 513 | + beforeEach(() => { |
| 514 | + WORLD.conflict = true; |
| 515 | + }); |
| 516 | + afterEach(() => { |
| 517 | + WORLD.conflict = false; |
| 518 | + }); |
| 519 | + |
| 520 | + it('a 409 with two candidates lists both, each linking to its package address', async () => { |
| 521 | + render(<Portal entry="/docs/shared_guide" />); |
| 522 | + const list = await candidateList(); |
| 523 | + expect(screen.getByRole('heading', { name: 'Choose a package' })).toBeInTheDocument(); |
| 524 | + const links = within(list).getAllByRole('link'); |
| 525 | + expect(links.map((l) => l.getAttribute('href'))).toEqual([ |
| 526 | + '/apps/com.example.alpha/docs/shared_guide', |
| 527 | + '/apps/com.example.beta/docs/shared_guide', |
| 528 | + ]); |
| 529 | + // The package as the portal names it (its book), with its id beside it. |
| 530 | + expect(links.map((l) => l.textContent)).toEqual(['Alphacom.example.alpha', 'Betacom.example.beta']); |
| 531 | + expect(screen.queryByTestId('doc-content')).toBeNull(); |
| 532 | + expect(screen.queryByText('Failed to load documentation')).toBeNull(); |
| 533 | + }); |
| 534 | + |
| 535 | + it('each candidate opens that package\'s doc, in that package\'s book', async () => { |
| 536 | + render(<Portal entry="/docs/shared_guide" />); |
| 537 | + fireEvent.click(within(await candidateList()).getByRole('link', { name: /com\.example\.alpha/ })); |
| 538 | + expect(await screen.findByTestId('doc-content')).toHaveTextContent('ALPHA SHARED BODY'); |
| 539 | + // Beta is listed first by the server; the package container decides the book. |
| 540 | + expect(screen.getByTestId('where').textContent).toBe('/apps/com.example.alpha/docs/com.example.alpha/shared_guide'); |
| 541 | + cleanup(); |
| 542 | + |
| 543 | + render(<Portal entry="/docs/shared_guide" />); |
| 544 | + fireEvent.click(within(await candidateList()).getByRole('link', { name: /com\.example\.beta/ })); |
| 545 | + expect(await screen.findByTestId('doc-content')).toHaveTextContent('BETA SHARED BODY'); |
| 546 | + expect(screen.getByTestId('where').textContent).toBe('/apps/com.example.beta/docs/com.example.beta/shared_guide'); |
| 547 | + }); |
| 548 | + |
| 549 | + it('CONTROL: a doc one package ships renders as today, with no candidate list', async () => { |
| 550 | + render(<Portal entry="/docs/alpha_welcome" />); |
| 551 | + expect(await screen.findByTestId('doc-content')).toHaveTextContent('ALPHA BODY'); |
| 552 | + expect(screen.getByTestId('where').textContent).toBe('/docs/com.example.alpha/alpha_welcome'); |
| 553 | + expect(screen.queryByRole('list', { name: 'Packages that provide this document' })).toBeNull(); |
| 554 | + }); |
| 555 | + |
| 556 | + it('CONTROL: a failure that is not the 409 keeps today\'s error state', async () => { |
| 557 | + render(<Portal entry="/docs/com.example.alpha/broken_doc" />); |
| 558 | + expect(await screen.findByText('Failed to load documentation')).toBeInTheDocument(); |
| 559 | + expect(screen.getByText('The metadata service is unavailable')).toBeInTheDocument(); |
| 560 | + expect(screen.queryByRole('list', { name: 'Packages that provide this document' })).toBeNull(); |
| 561 | + }); |
| 562 | +}); |
0 commit comments