| 1 | import { mkdir, mkdtemp, readFile, writeFile } from 'node:fs/promises' |
| 2 | import os from 'node:os' |
| 3 | import path from 'node:path' |
| 4 | import { describe, expect, it, vi } from 'vitest' |
| 5 | import { resolveSourceDocuments } from '../../../src/main/generation/source-documents' |
| 6 | |
| 7 | describe('resolveSourceDocuments', () => { |
| 8 | it('keeps the session reference document across edit-like modes', async () => { |
| 9 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-source-docs-')) |
| 10 | await mkdir(path.join(projectDir, 'docs'), { recursive: true }) |
| 11 | await writeFile(path.join(projectDir, 'docs', 'reference.md'), '# Reference\n', 'utf8') |
| 12 | |
| 13 | const result = await resolveSourceDocuments( |
| 14 | { localFiles: { assertPathInAllowedRoots: vi.fn() } } as any, |
| 15 | { |
| 16 | sessionId: 's1', |
| 17 | projectDir, |
| 18 | rawDocPaths: [], |
| 19 | mode: 'edit', |
| 20 | sessionRecord: { referenceDocumentPath: '/docs/reference.md' } |
| 21 | } |
| 22 | ) |
| 23 | |
| 24 | expect(result).toEqual(['/docs/reference.md']) |
| 25 | }) |
| 26 | |
| 27 | it('merges newly attached documents with the existing session reference document', async () => { |
| 28 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-source-docs-')) |
| 29 | const uploadDir = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-upload-docs-')) |
| 30 | await mkdir(path.join(projectDir, 'docs'), { recursive: true }) |
| 31 | await writeFile(path.join(projectDir, 'docs', 'reference.md'), '# Reference\n', 'utf8') |
| 32 | const uploadedPath = path.join(uploadDir, 'extra.md') |
| 33 | await writeFile(uploadedPath, '# Extra\n', 'utf8') |
| 34 | const assertPathInAllowedRoots = vi.fn(async () => uploadedPath) |
| 35 | |
| 36 | const result = await resolveSourceDocuments( |
| 37 | { localFiles: { assertPathInAllowedRoots } } as any, |
| 38 | { |
| 39 | sessionId: 's1', |
| 40 | projectDir, |
| 41 | rawDocPaths: [uploadedPath], |
| 42 | mode: 'edit', |
| 43 | sessionRecord: { referenceDocumentPath: '/docs/reference.md' } |
| 44 | } |
| 45 | ) |
| 46 | |
| 47 | expect(result).toEqual(['/docs/reference.md', '/docs/extra.md']) |
| 48 | expect(await readFile(path.join(projectDir, 'docs', 'extra.md'), 'utf8')).toBe('# Extra\n') |
| 49 | }) |
| 50 | |
| 51 | it('accepts legacy absolute reference paths only when they point inside project docs', async () => { |
| 52 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-source-docs-')) |
| 53 | await mkdir(path.join(projectDir, 'docs'), { recursive: true }) |
| 54 | const absoluteReferencePath = path.join(projectDir, 'docs', 'legacy.md') |
| 55 | await writeFile(absoluteReferencePath, '# Legacy\n', 'utf8') |
| 56 | |
| 57 | const result = await resolveSourceDocuments( |
| 58 | { localFiles: { assertPathInAllowedRoots: vi.fn() } } as any, |
| 59 | { |
| 60 | sessionId: 's1', |
| 61 | projectDir, |
| 62 | rawDocPaths: [], |
| 63 | mode: 'retrySinglePage', |
| 64 | sessionRecord: { reference_document_path: absoluteReferencePath } |
| 65 | } |
| 66 | ) |
| 67 | |
| 68 | expect(result).toEqual(['/docs/legacy.md']) |
| 69 | }) |
| 70 | }) |
| 71 |