|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { test } from 'node:test' |
| 3 | +import { createIndexApi } from '../src/api/index-api.ts' |
| 4 | + |
| 5 | +test('metadata endpoint returns the metadata field as the response body', async () => { |
| 6 | + const app = createIndexApi(async (dataSetId, pieceId) => { |
| 7 | + assert.equal(dataSetId, 123n) |
| 8 | + assert.equal(pieceId, 42n) |
| 9 | + return { name: 'example', type: 'application/octet-stream' } |
| 10 | + }) |
| 11 | + |
| 12 | + const response = await app.request('/index/v0/datasets/123/pieces/42/metadata') |
| 13 | + |
| 14 | + assert.equal(response.status, 200) |
| 15 | + assert.deepEqual(await response.json(), { name: 'example', type: 'application/octet-stream' }) |
| 16 | +}) |
| 17 | + |
| 18 | +test('metadata endpoint distinguishes absent metadata from an unknown piece', async () => { |
| 19 | + const withoutMetadata = createIndexApi(async () => null) |
| 20 | + const missingPiece = createIndexApi(async () => undefined) |
| 21 | + |
| 22 | + const withoutMetadataResponse = await withoutMetadata.request('/index/v0/datasets/123/pieces/42/metadata') |
| 23 | + const missingPieceResponse = await missingPiece.request('/index/v0/datasets/123/pieces/42/metadata') |
| 24 | + |
| 25 | + assert.equal(withoutMetadataResponse.status, 200) |
| 26 | + assert.equal(await withoutMetadataResponse.json(), null) |
| 27 | + assert.equal(missingPieceResponse.status, 404) |
| 28 | + assert.deepEqual(await missingPieceResponse.json(), { error: 'Piece not found' }) |
| 29 | +}) |
| 30 | + |
| 31 | +test('metadata endpoint rejects non-canonical unsigned IDs before querying', async () => { |
| 32 | + let queryCount = 0 |
| 33 | + const app = createIndexApi(async () => { |
| 34 | + queryCount++ |
| 35 | + return null |
| 36 | + }) |
| 37 | + |
| 38 | + for (const path of [ |
| 39 | + '/index/v0/datasets/-1/pieces/42/metadata', |
| 40 | + '/index/v0/datasets/01/pieces/42/metadata', |
| 41 | + '/index/v0/datasets/123/pieces/not-a-number/metadata', |
| 42 | + '/index/v0/datasets/9223372036854775808/pieces/42/metadata', |
| 43 | + `/index/v0/datasets/${'9'.repeat(1_000)}/pieces/42/metadata`, |
| 44 | + ]) { |
| 45 | + const response = await app.request(path) |
| 46 | + assert.equal(response.status, 400) |
| 47 | + assert.deepEqual(await response.json(), { error: 'dataSetId and pieceId must be unsigned int8 values' }) |
| 48 | + } |
| 49 | + |
| 50 | + assert.equal(queryCount, 0) |
| 51 | +}) |
| 52 | + |
| 53 | +test('metadata endpoint does not disclose query errors', async () => { |
| 54 | + const app = createIndexApi(async () => { |
| 55 | + throw new Error('database connection string leaked') |
| 56 | + }) |
| 57 | + const consoleError = console.error |
| 58 | + console.error = () => undefined |
| 59 | + |
| 60 | + try { |
| 61 | + const response = await app.request('/index/v0/datasets/123/pieces/42/metadata') |
| 62 | + |
| 63 | + assert.equal(response.status, 500) |
| 64 | + assert.deepEqual(await response.json(), { error: 'Internal server error' }) |
| 65 | + } finally { |
| 66 | + console.error = consoleError |
| 67 | + } |
| 68 | +}) |
0 commit comments