forked from filecoin-project/filecoin-pin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.test.ts
More file actions
532 lines (454 loc) · 16.1 KB
/
Copy pathimport.test.ts
File metadata and controls
532 lines (454 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/**
* Unit tests for CAR import functionality
*
* Tests the import command's ability to:
* - Validate CAR files
* - Handle various root CID scenarios
* - Initialize Synapse with progress callbacks
* - Upload to Filecoin
* - Clean up resources properly
*/
import { createWriteStream } from 'node:fs'
import { mkdir, rm, stat, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { pipeline } from 'node:stream/promises'
import { CarWriter } from '@ipld/car'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
import { sha256 } from 'multiformats/hashes/sha2'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { runCarImport } from '../../import/import.js'
import type { ImportOptions } from '../../import/types.js'
// Test constants
const ZERO_CID = 'bafkqaaa' // Zero CID used when CAR has no roots
// Mock modules
vi.mock('@filoz/synapse-sdk', async () => await import('../mocks/synapse-sdk.js'))
vi.mock('../../common/upload-flow.js', () => ({
validatePaymentSetup: vi.fn(),
performUpload: vi.fn().mockResolvedValue({
pieceCid: 'bafkzcibtest1234567890',
pieceId: 789,
dataSetId: '123',
network: 'calibration',
transactionHash: '0xabc123',
providerInfo: {
id: 1,
name: 'Test Provider',
serviceURL: 'http://test.provider',
},
}),
displayUploadResults: vi.fn(),
performAutoFunding: vi.fn(),
}))
vi.mock('../../core/payments/index.js', async () => {
const actual = await vi.importActual<typeof import('../../core/payments/index.js')>('../../core/payments/index.js')
return {
...actual,
checkFILBalance: vi.fn().mockResolvedValue({
balance: 1000000000000000000n,
isCalibnet: true,
hasSufficientGas: true,
}),
checkUSDFCBalance: vi.fn().mockResolvedValue(1000000000000000000000n),
checkAllowances: vi.fn().mockResolvedValue({
needsUpdate: false,
currentAllowances: {
rateAllowance: BigInt('0xffffffffffffffff'),
lockupAllowance: BigInt('0xffffffffffffffff'),
rateUsed: 0n,
lockupUsed: 0n,
},
}),
setMaxAllowances: vi.fn().mockResolvedValue({
transactionHash: '0x123...',
currentAllowances: {
rateAllowance: BigInt('0xffffffffffffffff'),
lockupAllowance: BigInt('0xffffffffffffffff'),
rateUsed: 0n,
lockupUsed: 0n,
},
}),
checkAndSetAllowances: vi.fn().mockResolvedValue({
updated: false,
currentAllowances: {
rateAllowance: BigInt('0xffffffffffffffff'),
lockupAllowance: BigInt('0xffffffffffffffff'),
rateUsed: 0n,
lockupUsed: 0n,
},
}),
validatePaymentCapacity: vi.fn().mockResolvedValue({
canUpload: true,
storageTiB: 0.001,
required: {
rateAllowance: 100000000000000n,
lockupAllowance: 1000000000000000000n,
storageCapacityTiB: 0.001,
},
issues: {},
suggestions: [],
}),
}
})
vi.mock('../../core/utils/validate-ipni-advertisement.js', () => ({
waitForIpniProviderResults: vi.fn().mockResolvedValue(true),
}))
vi.mock('../../payments/setup.js', () => ({
formatUSDFC: vi.fn((amount) => `${amount} USDFC`),
validatePaymentRequirements: vi.fn().mockReturnValue({ isValid: true }),
}))
vi.mock('../../core/synapse/index.js', async () => {
const { MockSynapse } = await import('../mocks/synapse-mocks.js')
return {
isSessionKeyMode: vi.fn(() => false),
initializeSynapse: vi.fn(async (config: any, _logger: any) => {
// Validate auth config (mirrors validateAuthConfig in actual code)
const hasStandardAuth = config.privateKey != null
const hasSessionKeyAuth = config.walletAddress != null && config.sessionKey != null
const hasViewOnlyAuth = config.readOnly === true && config.walletAddress != null
if (!hasStandardAuth && !hasSessionKeyAuth && !hasViewOnlyAuth) {
throw new Error(
'Authentication required: provide either privateKey, walletAddress + sessionKey, view-address, or signer'
)
}
const mockSynapse = new MockSynapse()
return mockSynapse
}),
createStorageContext: vi.fn(async (_synapse: any, _logger: any, options?: any) => {
const mockSynapse = new MockSynapse()
// Simulate progress callbacks
if (options?.callbacks) {
// Simulate provider selection
setTimeout(() => {
options.callbacks.onProviderSelected?.({
id: 1,
name: 'Mock Provider',
serviceProvider: '0x1234567890123456789012345678901234567890',
})
}, 10)
// Simulate dataset resolution
setTimeout(() => {
options.callbacks.onDataSetResolved?.({
dataSetId: 123,
isExisting: false,
})
}, 20)
}
const mockStorage = await mockSynapse.storage.createContext()
return {
synapse: mockSynapse as any,
storage: mockStorage,
providerInfo: {
id: 1,
name: 'Mock Provider',
serviceProvider: '0x1234567890123456789012345678901234567890',
products: {
PDP: {
data: {
serviceURL: 'http://localhost:8888/pdp',
},
},
},
},
}
}),
cleanupSynapseService: vi.fn(async () => {
// Mock cleanup
}),
}
})
// Mock console methods to capture output
const consoleMocks = {
log: vi.spyOn(console, 'log').mockImplementation(() => {
// Intentionally empty - suppressing console output in tests
}),
error: vi.spyOn(console, 'error').mockImplementation(() => {
// Intentionally empty - suppressing console output in tests
}),
}
// Mock process.exit to prevent test runner from exiting
const processExitMock = vi.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('process.exit called')
})
/**
* Create a test CAR file with the given content
*
* @param filePath - Path where the CAR file should be written
* @param roots - Root CIDs for the CAR header
* @param blocks - Array of { content, cid? } to add to the CAR
*/
async function createTestCarFile(
filePath: string,
roots: CID[],
blocks: Array<{ content: string | Uint8Array; cid?: CID }> = []
): Promise<{ cids: CID[] }> {
const { writer, out } = await CarWriter.create(roots)
const writeStream = createWriteStream(filePath)
// Start piping the CAR output to file
const pipePromise = pipeline(out as any, writeStream)
// Track CIDs for test assertions
const cids: CID[] = []
// Write blocks
for (const block of blocks) {
const bytes = typeof block.content === 'string' ? new TextEncoder().encode(block.content) : block.content
let cid = block.cid
if (!cid) {
const hash = await sha256.digest(bytes)
cid = CID.create(1, raw.code, hash)
}
cids.push(cid)
await writer.put({ cid, bytes })
}
await writer.close()
await pipePromise
return { cids }
}
describe('CAR Import', () => {
const testDir = './test-import-cars'
const testPrivateKey = '0x0000000000000000000000000000000000000000000000000000000000000001'
beforeEach(async () => {
// Create test directory
await mkdir(testDir, { recursive: true })
// Clear all mocks
vi.clearAllMocks()
consoleMocks.log.mockClear()
consoleMocks.error.mockClear()
processExitMock.mockClear()
})
afterEach(async () => {
// Clean up test files
try {
await stat(testDir)
await rm(testDir, { recursive: true, force: true })
} catch {
// Directory doesn't exist, nothing to clean up
}
})
describe('CAR File Validation', () => {
it('should validate a proper CAR file with single root', async () => {
const carPath = join(testDir, 'valid.car')
const { cids } = await createTestCarFile(
carPath,
[], // Will use first block's CID as root
[{ content: 'test content' }]
)
const cid = cids[0]
if (!cid) throw new Error('No CID generated')
// Update CAR with proper root
await createTestCarFile(carPath, [cid], [{ content: 'test content', cid }])
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
const result = await runCarImport(options)
expect(result.rootCid).toBe(cid.toString())
expect(result.filePath).toBe(carPath)
expect(result.pieceCid).toBeDefined()
expect(result.dataSetId).toBeDefined()
})
it('should handle CAR file with no roots (use zero CID)', async () => {
const carPath = join(testDir, 'no-roots.car')
await createTestCarFile(
carPath,
[], // Empty roots array
[{ content: 'test content' }]
)
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
const result = await runCarImport(options)
expect(result.rootCid).toBe(ZERO_CID) // Zero CID
expect(result.filePath).toBe(carPath)
expect(result.pieceCid).toBeDefined()
})
it('should handle CAR file with multiple roots (use first)', async () => {
const carPath = join(testDir, 'multi-roots.car')
const { cids } = await createTestCarFile(
carPath,
[], // Will set roots after creating CIDs
[{ content: 'content 1' }, { content: 'content 2' }]
)
const cid1 = cids[0]
const cid2 = cids[1]
if (!cid1 || !cid2) throw new Error('CIDs not generated')
// Recreate with multiple roots
await createTestCarFile(
carPath,
[cid1, cid2], // Multiple roots
[
{ content: 'content 1', cid: cid1 },
{ content: 'content 2', cid: cid2 },
]
)
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
const result = await runCarImport(options)
expect(result.rootCid).toBe(cid1.toString()) // Should use first CID
expect(consoleMocks.log).toHaveBeenCalledWith(expect.stringContaining('Multiple root CIDs found'))
})
it('should reject invalid CAR file', async () => {
const invalidCarPath = join(testDir, 'invalid.car')
await writeFile(invalidCarPath, 'not a car file')
const options: ImportOptions = {
filePath: invalidCarPath,
privateKey: testPrivateKey,
}
await expect(runCarImport(options)).rejects.toThrow()
})
it('should reject non-existent file', async () => {
const options: ImportOptions = {
filePath: join(testDir, 'nonexistent.car'),
privateKey: testPrivateKey,
}
await expect(runCarImport(options)).rejects.toThrow()
})
})
describe('Synapse Integration', () => {
it('should show progress during initialization', async () => {
const carPath = join(testDir, 'progress.car')
await createTestCarFile(
carPath,
[], // Will use first block's CID
[{ content: 'test content' }]
)
const { createStorageContext } = await import('../../core/synapse/index.js')
const createContextSpy = vi.mocked(createStorageContext)
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
await runCarImport(options)
// Verify progress callbacks were provided to createStorageContext
expect(createContextSpy).toHaveBeenCalledWith(
expect.any(Object), // synapse
expect.objectContaining({
logger: expect.anything(),
callbacks: expect.objectContaining({
onProviderSelected: expect.any(Function),
onDataSetResolved: expect.any(Function),
}),
})
)
})
it('should require private key', async () => {
const carPath = join(testDir, 'test.car')
await createTestCarFile(carPath, [], [{ content: 'test content' }])
const options: ImportOptions = {
filePath: carPath,
// No private key provided
}
await expect(runCarImport(options)).rejects.toThrow()
// The error is caught and logged generically as "Import failed"
expect(consoleMocks.error).toHaveBeenCalled()
})
it('should use custom RPC URL if provided', async () => {
const carPath = join(testDir, 'rpc.car')
await createTestCarFile(carPath, [], [{ content: 'test content' }])
const customRpcUrl = 'wss://custom.rpc.url/ws'
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
rpcUrl: customRpcUrl,
}
const { initializeSynapse } = await import('../../core/synapse/index.js')
const initSpy = vi.mocked(initializeSynapse)
await runCarImport(options)
expect(initSpy).toHaveBeenCalledWith(
expect.objectContaining({
rpcUrl: customRpcUrl,
}),
expect.any(Object)
)
})
it('passes metadata options through to upload and storage context', async () => {
const carPath = join(testDir, 'metadata.car')
await createTestCarFile(carPath, [], [{ content: 'test content' }])
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
pieceMetadata: { ics: '8004' },
dataSetMetadata: { erc8004Files: '' },
}
await runCarImport(options)
const { createStorageContext, initializeSynapse } = await import('../../core/synapse/index.js')
expect(vi.mocked(initializeSynapse)).toHaveBeenCalledWith(
expect.objectContaining({
dataSetMetadata: { erc8004Files: '' },
}),
expect.any(Object)
)
expect(vi.mocked(createStorageContext)).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
dataset: {
metadata: { erc8004Files: '' },
},
logger: expect.anything(),
})
)
const { performUpload } = await import('../../common/upload-flow.js')
expect(vi.mocked(performUpload)).toHaveBeenCalledWith(
expect.any(Object),
expect.any(Uint8Array),
expect.any(Object),
expect.objectContaining({
pieceMetadata: { ics: '8004' },
})
)
})
})
describe('Cleanup', () => {
it('should call cleanup on success', async () => {
const carPath = join(testDir, 'cleanup.car')
await createTestCarFile(carPath, [], [{ content: 'test content' }])
const { cleanupSynapseService } = await import('../../core/synapse/index.js')
const cleanupSpy = vi.mocked(cleanupSynapseService)
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
await runCarImport(options)
expect(cleanupSpy).toHaveBeenCalled()
})
it('should call cleanup on error', async () => {
const { cleanupSynapseService } = await import('../../core/synapse/index.js')
const cleanupSpy = vi.mocked(cleanupSynapseService)
const options: ImportOptions = {
filePath: 'nonexistent.car',
privateKey: testPrivateKey,
}
await expect(runCarImport(options)).rejects.toThrow()
expect(cleanupSpy).toHaveBeenCalled()
})
})
describe('Upload Result', () => {
it('should return complete import result', async () => {
const carPath = join(testDir, 'result.car')
const { cids } = await createTestCarFile(carPath, [], [{ content: 'test content' }])
const cid = cids[0]
if (!cid) throw new Error('No CID generated')
// Recreate with proper root
await createTestCarFile(carPath, [cid], [{ content: 'test content', cid }])
const options: ImportOptions = {
filePath: carPath,
privateKey: testPrivateKey,
}
const result = await runCarImport(options)
expect(result).toMatchObject({
filePath: carPath,
fileSize: expect.any(Number),
rootCid: cid.toString(),
pieceCid: expect.stringMatching(/^bafkzcib/), // CommP prefix
pieceId: expect.any(Number),
dataSetId: '123', // Mock returns string
})
// Provider info is always present
expect(result.providerInfo).toBeDefined()
expect(result.providerInfo.id).toBe(1)
expect(result.providerInfo.name).toBe('Mock Provider')
})
})
})