|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { readAtlassian, searchAtlassian } from '@/lib/sim-search/live/atlassian' |
| 3 | +import type { NativeClient } from '@/lib/sim-search/live/types' |
| 4 | + |
| 5 | +const SITE = { id: 'cloud', url: 'https://acme.atlassian.net' } |
| 6 | + |
| 7 | +/** Answers only the paths a test names, so a read through the v1 content API fails loudly. */ |
| 8 | +function client(rows: Record<string, unknown>): NativeClient & { json: ReturnType<typeof vi.fn> } { |
| 9 | + return { |
| 10 | + json: vi.fn(async (path: string) => { |
| 11 | + if (path === '/oauth/token/accessible-resources') return [SITE] |
| 12 | + if (!(path in rows)) throw new Error(`Unexpected request: ${path}`) |
| 13 | + return rows[path] |
| 14 | + }), |
| 15 | + text: vi.fn(), |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const v2 = '/ex/confluence/cloud/wiki/api/v2' |
| 20 | + |
| 21 | +describe('Confluence live documents', () => { |
| 22 | + it('reads pages and blog posts through v2, which needs only the granular read scopes', async () => { |
| 23 | + const api = client({ |
| 24 | + [`${v2}/pages/123`]: { |
| 25 | + id: '123', |
| 26 | + title: 'Runbook', |
| 27 | + body: { view: { value: '<p>Restart the <b>ingest</b> worker.</p>' } }, |
| 28 | + version: { createdAt: '2026-09-18T04:50:29.778Z' }, |
| 29 | + _links: { webui: '/spaces/ENG/pages/123/Runbook' }, |
| 30 | + }, |
| 31 | + [`${v2}/blogposts/9`]: { |
| 32 | + id: '9', |
| 33 | + title: 'Release notes', |
| 34 | + body: { view: { value: '<p>Shipped search.</p>' } }, |
| 35 | + version: { createdAt: '2026-09-20T00:00:00.000Z' }, |
| 36 | + _links: { webui: '/spaces/ENG/blog/9' }, |
| 37 | + }, |
| 38 | + }) |
| 39 | + await expect(readAtlassian(api, 'confluence', '123', 'cloud', 'page')).resolves.toMatchObject({ |
| 40 | + id: '123', |
| 41 | + kind: 'page', |
| 42 | + title: 'Runbook', |
| 43 | + content: expect.stringContaining('Restart the ingest worker.'), |
| 44 | + url: 'https://acme.atlassian.net/wiki/spaces/ENG/pages/123/Runbook', |
| 45 | + modifiedAt: '2026-09-18T04:50:29.778Z', |
| 46 | + }) |
| 47 | + await expect(readAtlassian(api, 'confluence', '9', 'cloud', 'blogpost')).resolves.toMatchObject( |
| 48 | + { |
| 49 | + kind: 'blogpost', |
| 50 | + content: expect.stringContaining('Shipped search.'), |
| 51 | + } |
| 52 | + ) |
| 53 | + expect(api.json).toHaveBeenCalledWith(`${v2}/pages/123`, { query: { 'body-format': 'view' } }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('reads a space result as its homepage, keeping the space as the document', async () => { |
| 57 | + const api = client({ |
| 58 | + [`${v2}/spaces`]: { |
| 59 | + results: [{ id: '7', key: 'ENG', name: 'Engineering', homepageId: '55' }], |
| 60 | + }, |
| 61 | + [`${v2}/pages/55`]: { |
| 62 | + id: '55', |
| 63 | + title: 'Engineering Home', |
| 64 | + body: { view: { value: '<p>Team charter.</p>' } }, |
| 65 | + _links: { webui: '/spaces/ENG/overview' }, |
| 66 | + }, |
| 67 | + }) |
| 68 | + await expect(readAtlassian(api, 'confluence', 'ENG', 'cloud', 'space')).resolves.toMatchObject({ |
| 69 | + id: 'ENG', |
| 70 | + kind: 'space', |
| 71 | + title: 'Engineering', |
| 72 | + content: expect.stringContaining('Team charter.'), |
| 73 | + }) |
| 74 | + expect(api.json).toHaveBeenCalledWith(`${v2}/spaces`, { query: { keys: 'ENG' } }) |
| 75 | + }) |
| 76 | + |
| 77 | + it('records whether a search result is a page, blog post, or space so its read picks the endpoint', async () => { |
| 78 | + const api = client({ |
| 79 | + '/ex/confluence/cloud/wiki/rest/api/search': { |
| 80 | + results: [ |
| 81 | + { |
| 82 | + content: { |
| 83 | + id: '123', |
| 84 | + type: 'page', |
| 85 | + title: 'Runbook', |
| 86 | + _links: { webui: '/spaces/ENG/pages/123' }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + content: { |
| 91 | + id: '9', |
| 92 | + type: 'blogpost', |
| 93 | + title: 'Release notes', |
| 94 | + _links: { webui: '/spaces/ENG/blog/9' }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + entityType: 'space', |
| 99 | + title: 'Engineering', |
| 100 | + url: '/spaces/ENG', |
| 101 | + space: { key: 'ENG', name: 'Engineering' }, |
| 102 | + }, |
| 103 | + ], |
| 104 | + _links: {}, |
| 105 | + }, |
| 106 | + }) |
| 107 | + const page = await searchAtlassian(api, 'confluence', { |
| 108 | + query: 'runbook', |
| 109 | + limit: 10, |
| 110 | + scopes: [], |
| 111 | + }) |
| 112 | + expect(page.documents.map(({ id, kind }) => ({ id, kind }))).toEqual([ |
| 113 | + { id: '123', kind: 'page' }, |
| 114 | + { id: '9', kind: 'blogpost' }, |
| 115 | + { id: 'ENG', kind: 'space' }, |
| 116 | + ]) |
| 117 | + expect(page.documents[2]?.url).toBe('https://acme.atlassian.net/wiki/spaces/ENG') |
| 118 | + }) |
| 119 | +}) |
0 commit comments