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