|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@sim/db', () => dbChainMock) |
| 9 | + |
| 10 | +const { mockReadFileRecord } = vi.hoisted(() => ({ |
| 11 | + mockReadFileRecord: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/copilot/vfs/file-reader', () => ({ |
| 15 | + readFileRecord: mockReadFileRecord, |
| 16 | +})) |
| 17 | + |
| 18 | +import { |
| 19 | + findMothershipUploadRowByChatAndName, |
| 20 | + listChatUploads, |
| 21 | + readChatUpload, |
| 22 | +} from './upload-file-reader' |
| 23 | + |
| 24 | +const CHAT_ID = '11111111-1111-1111-1111-111111111111' |
| 25 | +const NOW = new Date('2026-05-05T00:00:00.000Z') |
| 26 | + |
| 27 | +function makeRow(overrides: Partial<Record<string, unknown>> = {}) { |
| 28 | + return { |
| 29 | + id: 'wf_1', |
| 30 | + key: 'mothership/abc/123-image.png', |
| 31 | + userId: 'user_1', |
| 32 | + workspaceId: 'ws_1', |
| 33 | + context: 'mothership', |
| 34 | + chatId: CHAT_ID, |
| 35 | + originalName: 'image.png', |
| 36 | + displayName: 'image.png', |
| 37 | + contentType: 'image/png', |
| 38 | + size: 1024, |
| 39 | + deletedAt: null, |
| 40 | + uploadedAt: NOW, |
| 41 | + updatedAt: NOW, |
| 42 | + ...overrides, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Resolver chain is `.where().orderBy(...).limit(1)`. The default chain mock makes |
| 48 | + * `orderBy` a terminal, so we wire a chainable `{limit}` for each call manually. |
| 49 | + */ |
| 50 | +function mockOrderByThenLimit(rows: unknown) { |
| 51 | + dbChainMockFns.orderBy.mockReturnValueOnce({ limit: dbChainMockFns.limit } as never) |
| 52 | + dbChainMockFns.limit.mockResolvedValueOnce(rows as never) |
| 53 | +} |
| 54 | + |
| 55 | +describe('findMothershipUploadRowByChatAndName', () => { |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks() |
| 58 | + resetDbChainMock() |
| 59 | + }) |
| 60 | + |
| 61 | + it('matches by displayName for the first occurrence', async () => { |
| 62 | + const row = makeRow({ id: 'wf_1', displayName: 'image.png' }) |
| 63 | + mockOrderByThenLimit([row]) |
| 64 | + |
| 65 | + const result = await findMothershipUploadRowByChatAndName(CHAT_ID, 'image.png') |
| 66 | + |
| 67 | + expect(result).toEqual(row) |
| 68 | + }) |
| 69 | + |
| 70 | + it('matches by suffixed displayName for collision-disambiguated rows', async () => { |
| 71 | + const row = makeRow({ id: 'wf_2', displayName: 'image (2).png' }) |
| 72 | + mockOrderByThenLimit([row]) |
| 73 | + |
| 74 | + const result = await findMothershipUploadRowByChatAndName(CHAT_ID, 'image (2).png') |
| 75 | + |
| 76 | + expect(result?.id).toBe('wf_2') |
| 77 | + expect(result?.displayName).toBe('image (2).png') |
| 78 | + }) |
| 79 | + |
| 80 | + it('prefers the most recent row when legacy rows share the same originalName', async () => { |
| 81 | + // Pre-displayName legacy rows have displayName=null. Resolver's ORDER BY uploaded_at |
| 82 | + // DESC ensures the newest upload wins, fixing read("uploads/<name>") for legacy data. |
| 83 | + const newer = makeRow({ |
| 84 | + id: 'wf_new', |
| 85 | + displayName: null, |
| 86 | + originalName: 'image.png', |
| 87 | + uploadedAt: new Date('2026-05-05T12:00:00.000Z'), |
| 88 | + }) |
| 89 | + mockOrderByThenLimit([newer]) |
| 90 | + |
| 91 | + const result = await findMothershipUploadRowByChatAndName(CHAT_ID, 'image.png') |
| 92 | + |
| 93 | + expect(result?.id).toBe('wf_new') |
| 94 | + }) |
| 95 | + |
| 96 | + it('returns null when no row matches and the fallback scan is empty', async () => { |
| 97 | + // First query: .where().orderBy().limit() returns []. |
| 98 | + mockOrderByThenLimit([]) |
| 99 | + // Second query: .where().orderBy(...) (no .limit) — orderBy is the terminal. |
| 100 | + dbChainMockFns.orderBy.mockResolvedValueOnce([] as never) |
| 101 | + |
| 102 | + const result = await findMothershipUploadRowByChatAndName(CHAT_ID, 'missing.png') |
| 103 | + |
| 104 | + expect(result).toBeNull() |
| 105 | + }) |
| 106 | + |
| 107 | + it('falls back to normalized segment match when exact lookup misses (macOS U+202F)', async () => { |
| 108 | + // Model passes ASCII space; DB row was saved with U+202F (narrow no-break space). |
| 109 | + const macosName = 'Screenshot 2026-05-05 at 9.41.00 AM.png' |
| 110 | + const asciiName = 'Screenshot 2026-05-05 at 9.41.00 AM.png' |
| 111 | + const row = makeRow({ id: 'wf_3', displayName: macosName }) |
| 112 | + |
| 113 | + mockOrderByThenLimit([]) |
| 114 | + dbChainMockFns.orderBy.mockResolvedValueOnce([row] as never) |
| 115 | + |
| 116 | + const result = await findMothershipUploadRowByChatAndName(CHAT_ID, asciiName) |
| 117 | + |
| 118 | + expect(result?.id).toBe('wf_3') |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +describe('listChatUploads', () => { |
| 123 | + beforeEach(() => { |
| 124 | + vi.clearAllMocks() |
| 125 | + resetDbChainMock() |
| 126 | + }) |
| 127 | + |
| 128 | + it('returns rows in upload order with name set to displayName', async () => { |
| 129 | + const rows = [ |
| 130 | + makeRow({ id: 'a', displayName: 'image.png' }), |
| 131 | + makeRow({ id: 'b', displayName: 'image (2).png' }), |
| 132 | + makeRow({ id: 'c', displayName: 'image (3).png' }), |
| 133 | + ] |
| 134 | + dbChainMockFns.orderBy.mockResolvedValueOnce(rows) |
| 135 | + |
| 136 | + const result = await listChatUploads(CHAT_ID) |
| 137 | + |
| 138 | + expect(result.map((r) => r.id)).toEqual(['a', 'b', 'c']) |
| 139 | + expect(result.map((r) => r.name)).toEqual(['image.png', 'image (2).png', 'image (3).png']) |
| 140 | + expect(result.every((r) => r.storageContext === 'mothership')).toBe(true) |
| 141 | + }) |
| 142 | + |
| 143 | + it('returns [] and does not throw when the DB query fails', async () => { |
| 144 | + dbChainMockFns.orderBy.mockRejectedValueOnce(new Error('boom')) |
| 145 | + const result = await listChatUploads(CHAT_ID) |
| 146 | + expect(result).toEqual([]) |
| 147 | + }) |
| 148 | +}) |
| 149 | + |
| 150 | +describe('readChatUpload', () => { |
| 151 | + beforeEach(() => { |
| 152 | + vi.clearAllMocks() |
| 153 | + resetDbChainMock() |
| 154 | + mockReadFileRecord.mockReset() |
| 155 | + }) |
| 156 | + |
| 157 | + it('reads the row resolved by the suffixed displayName', async () => { |
| 158 | + const row = makeRow({ id: 'wf_2', displayName: 'image (2).png' }) |
| 159 | + mockOrderByThenLimit([row]) |
| 160 | + mockReadFileRecord.mockResolvedValueOnce({ content: 'PNGDATA', totalLines: 1 }) |
| 161 | + |
| 162 | + const result = await readChatUpload('image (2).png', CHAT_ID) |
| 163 | + |
| 164 | + expect(result).toEqual({ content: 'PNGDATA', totalLines: 1 }) |
| 165 | + expect(mockReadFileRecord).toHaveBeenCalledWith( |
| 166 | + expect.objectContaining({ id: 'wf_2', name: 'image (2).png', storageContext: 'mothership' }) |
| 167 | + ) |
| 168 | + }) |
| 169 | + |
| 170 | + it('returns null when no row matches', async () => { |
| 171 | + mockOrderByThenLimit([]) |
| 172 | + dbChainMockFns.orderBy.mockResolvedValueOnce([] as never) |
| 173 | + |
| 174 | + const result = await readChatUpload('nope.png', CHAT_ID) |
| 175 | + |
| 176 | + expect(result).toBeNull() |
| 177 | + expect(mockReadFileRecord).not.toHaveBeenCalled() |
| 178 | + }) |
| 179 | +}) |
0 commit comments