|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// ─── Mocks ───────────────────────────────────────────────────────────────── |
| 4 | + |
| 5 | +const mockWaitAsync = vi.fn(); |
| 6 | +const mockFilterByType = vi.fn(); |
| 7 | +const mockCollectAsync = vi.fn(); |
| 8 | + |
| 9 | +const mockHandle = { |
| 10 | + waitAsync: mockWaitAsync, |
| 11 | + filterByType: mockFilterByType, |
| 12 | + collectAsync: mockCollectAsync, |
| 13 | +}; |
| 14 | +mockFilterByType.mockReturnValue(mockHandle); |
| 15 | + |
| 16 | +const mockLore = { |
| 17 | + logConfigure: vi.fn(), |
| 18 | + shutdown: vi.fn(), |
| 19 | + repositoryCreate: vi.fn(() => mockHandle), |
| 20 | + fileStage: vi.fn(() => mockHandle), |
| 21 | + revisionCommit: vi.fn(() => mockHandle), |
| 22 | +}; |
| 23 | + |
| 24 | +vi.mock('@lore-vcs/sdk', () => ({ lore: mockLore })); |
| 25 | +vi.mock('@lore-vcs/sdk/types/enums', () => ({ |
| 26 | + LoreEventTag: { REVISION_COMMIT_REVISION: 'REVISION_COMMIT_REVISION' }, |
| 27 | + LoreLogLevel: { WARN: 'WARN' }, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('@imajin/logger', () => ({ |
| 31 | + createLogger: vi.fn(() => ({ info: vi.fn(), error: vi.fn(), warn: vi.fn() })), |
| 32 | +})); |
| 33 | + |
| 34 | +// Fake the .lore directory check so we don't need a real filesystem |
| 35 | +vi.mock('node:fs', () => ({ |
| 36 | + existsSync: vi.fn(() => false), // pretend .lore doesn't exist → triggers repositoryCreate |
| 37 | +})); |
| 38 | + |
| 39 | +// ─── Subject ─────────────────────────────────────────────────────────────── |
| 40 | + |
| 41 | +// Import AFTER mocks are registered |
| 42 | +const { LoreBlobStore } = await import('../blob-store-lore'); |
| 43 | + |
| 44 | +// ─── Helpers ─────────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | +const FAKE_REV = 'a'.repeat(64); |
| 47 | +const OWNER_DID = 'did:imajin:owner123'; |
| 48 | +const FILE_PATH = '/mnt/media/did_imajin_owner123/assets/asset_abc.mp4'; |
| 49 | +const HINT = { assetId: 'asset_abc', sizeBytes: 512_000 }; |
| 50 | + |
| 51 | +function setupSuccessfulCommit(revision = FAKE_REV) { |
| 52 | + mockWaitAsync.mockResolvedValue(0); |
| 53 | + mockCollectAsync.mockResolvedValue([ |
| 54 | + { tag: 'REVISION_COMMIT_REVISION', data: { revision } }, |
| 55 | + ]); |
| 56 | +} |
| 57 | + |
| 58 | +// ─── Tests ───────────────────────────────────────────────────────────────── |
| 59 | + |
| 60 | +describe('LoreBlobStore', () => { |
| 61 | + let store: InstanceType<typeof LoreBlobStore>; |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + vi.clearAllMocks(); |
| 65 | + mockFilterByType.mockReturnValue(mockHandle); |
| 66 | + store = new LoreBlobStore(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('put returns a 64-char loreRef on success', async () => { |
| 70 | + setupSuccessfulCommit(); |
| 71 | + |
| 72 | + const ref = await store.put(OWNER_DID, FILE_PATH, HINT); |
| 73 | + |
| 74 | + expect(ref.loreRef).toBe(FAKE_REV); |
| 75 | + expect(ref.loreRef).toHaveLength(64); |
| 76 | + expect(ref.sizeBytes).toBe(HINT.sizeBytes); |
| 77 | + }); |
| 78 | + |
| 79 | + it('put calls repositoryCreate when .lore directory does not exist', async () => { |
| 80 | + setupSuccessfulCommit(); |
| 81 | + |
| 82 | + await store.put(OWNER_DID, FILE_PATH, HINT); |
| 83 | + |
| 84 | + expect(mockLore.repositoryCreate).toHaveBeenCalledOnce(); |
| 85 | + expect(mockLore.repositoryCreate).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ offline: true }), |
| 87 | + expect.objectContaining({ repositoryUrl: expect.stringContaining('imajin-media') }), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('put skips repositoryCreate on second call for the same DID (in-process cache)', async () => { |
| 92 | + setupSuccessfulCommit(); |
| 93 | + |
| 94 | + await store.put(OWNER_DID, FILE_PATH, HINT); |
| 95 | + await store.put(OWNER_DID, FILE_PATH, { ...HINT, assetId: 'asset_def' }); |
| 96 | + |
| 97 | + // repositoryCreate should only be called once across both puts |
| 98 | + expect(mockLore.repositoryCreate).toHaveBeenCalledOnce(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('put calls fileStage with the correct filePath', async () => { |
| 102 | + setupSuccessfulCommit(); |
| 103 | + |
| 104 | + await store.put(OWNER_DID, FILE_PATH, HINT); |
| 105 | + |
| 106 | + expect(mockLore.fileStage).toHaveBeenCalledWith( |
| 107 | + expect.objectContaining({ repositoryPath: expect.stringContaining('did_imajin_owner123') }), |
| 108 | + { paths: [FILE_PATH] }, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('put includes assetId in the commit message', async () => { |
| 113 | + setupSuccessfulCommit(); |
| 114 | + |
| 115 | + await store.put(OWNER_DID, FILE_PATH, HINT); |
| 116 | + |
| 117 | + expect(mockLore.revisionCommit).toHaveBeenCalledWith( |
| 118 | + expect.any(Object), |
| 119 | + { message: `upload: ${HINT.assetId}` }, |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('put throws when revision hash is missing from commit events', async () => { |
| 124 | + mockWaitAsync.mockResolvedValue(0); |
| 125 | + mockCollectAsync.mockResolvedValue([]); // no REVISION_COMMIT_REVISION event |
| 126 | + |
| 127 | + await expect(store.put(OWNER_DID, FILE_PATH, HINT)).rejects.toThrow( |
| 128 | + 'did not return a valid revision hash', |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('put throws when revision hash is not 64 chars', async () => { |
| 133 | + mockWaitAsync.mockResolvedValue(0); |
| 134 | + mockCollectAsync.mockResolvedValue([ |
| 135 | + { tag: 'REVISION_COMMIT_REVISION', data: { revision: 'tooshort' } }, |
| 136 | + ]); |
| 137 | + |
| 138 | + await expect(store.put(OWNER_DID, FILE_PATH, HINT)).rejects.toThrow( |
| 139 | + 'did not return a valid revision hash', |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('put propagates errors so the upload route .catch() can handle them', async () => { |
| 144 | + mockWaitAsync.mockRejectedValue(new Error('Lore I/O error')); |
| 145 | + |
| 146 | + await expect(store.put(OWNER_DID, FILE_PATH, HINT)).rejects.toThrow('Lore I/O error'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('serializes same-DID concurrent puts (queue concurrency=1)', async () => { |
| 150 | + const order: number[] = []; |
| 151 | + |
| 152 | + // Each put takes a different amount of time to complete |
| 153 | + mockLore.repositoryCreate.mockReturnValue(mockHandle); |
| 154 | + mockWaitAsync.mockImplementation(() => Promise.resolve(0)); |
| 155 | + |
| 156 | + let call = 0; |
| 157 | + mockCollectAsync.mockImplementation(() => { |
| 158 | + const n = ++call; |
| 159 | + return new Promise(resolve => |
| 160 | + setTimeout(() => { |
| 161 | + order.push(n); |
| 162 | + resolve([{ tag: 'REVISION_COMMIT_REVISION', data: { revision: FAKE_REV } }]); |
| 163 | + }, n === 1 ? 30 : 10) // first put is slower |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + // Fire both at the same time |
| 168 | + const [r1, r2] = await Promise.all([ |
| 169 | + store.put(OWNER_DID, FILE_PATH, { ...HINT, assetId: 'asset_1' }), |
| 170 | + store.put(OWNER_DID, FILE_PATH, { ...HINT, assetId: 'asset_2' }), |
| 171 | + ]); |
| 172 | + |
| 173 | + // Both succeed |
| 174 | + expect(r1.loreRef).toBe(FAKE_REV); |
| 175 | + expect(r2.loreRef).toBe(FAKE_REV); |
| 176 | + // Serialized: 1 completes before 2 starts |
| 177 | + expect(order).toEqual([1, 2]); |
| 178 | + }); |
| 179 | + |
| 180 | + it('runs different-DID puts concurrently (independent queues)', async () => { |
| 181 | + const completed: string[] = []; |
| 182 | + |
| 183 | + mockWaitAsync.mockResolvedValue(0); |
| 184 | + let call = 0; |
| 185 | + mockCollectAsync.mockImplementation(() => { |
| 186 | + const n = ++call; |
| 187 | + const did = n === 1 ? 'did:imajin:alice' : 'did:imajin:bob'; |
| 188 | + return new Promise(resolve => |
| 189 | + setTimeout(() => { |
| 190 | + completed.push(did); |
| 191 | + resolve([{ tag: 'REVISION_COMMIT_REVISION', data: { revision: FAKE_REV } }]); |
| 192 | + }, n === 1 ? 20 : 5) |
| 193 | + ); |
| 194 | + }); |
| 195 | + |
| 196 | + await Promise.all([ |
| 197 | + store.put('did:imajin:alice', FILE_PATH, HINT), |
| 198 | + store.put('did:imajin:bob', FILE_PATH, HINT), |
| 199 | + ]); |
| 200 | + |
| 201 | + // bob (faster) should finish before alice (slower) — they ran concurrently |
| 202 | + expect(completed[0]).toBe('did:imajin:bob'); |
| 203 | + expect(completed[1]).toBe('did:imajin:alice'); |
| 204 | + }); |
| 205 | + |
| 206 | + it('gc is a no-op and does not throw', async () => { |
| 207 | + await expect(store.gc(OWNER_DID, FAKE_REV)).resolves.toBeUndefined(); |
| 208 | + }); |
| 209 | +}); |
0 commit comments