Files
pokemon-inventory/apps/api/tests/application/image-storage.test.js
T
fragginwagon c728016dec feat: establish auth listing and image vertical slices
Includes MFA-capable authentication, session and CSRF security, password recovery, listing lifecycle and ownership rules, inventory image metadata, local ImageStorage, migrations, tests, and implementation status documentation. Verification: npm test (62 passing) and integration suite (14 passing).
2026-09-08 17:32:03 -04:00

48 lines
1.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { createImageStorageContract } from '../../src/infrastructure/images/image-storage.js';
import { createLocalImageStorage } from '../../src/infrastructure/images/local-image-storage.js';
test('local image storage round-trips and deletes binary data', async () => {
const root = await mkdtemp(path.join(os.tmpdir(), 'pokemon-images-'));
const storage = createLocalImageStorage(root);
try {
await storage.put({ key: 'inventory/item-1/front.jpg', data: Buffer.from('image-bytes') });
assert.deepEqual(await storage.get('inventory/item-1/front.jpg'), Buffer.from('image-bytes'));
await storage.delete('inventory/item-1/front.jpg');
await assert.rejects(() => storage.get('inventory/item-1/front.jpg'), { code: 'ENOENT' });
} finally {
await rm(root, { recursive: true, force: true });
}
});
test('local image storage rejects traversal and duplicate writes', async () => {
const root = await mkdtemp(path.join(os.tmpdir(), 'pokemon-images-'));
const storage = createLocalImageStorage(root);
try {
await assert.rejects(
() => storage.put({ key: '../outside.jpg', data: Buffer.from('image-bytes') }),
/escapes the storage root/,
);
await storage.put({ key: 'same.jpg', data: Buffer.from('first') });
await assert.rejects(
() => storage.put({ key: 'same.jpg', data: Buffer.from('second') }),
{ code: 'EEXIST' },
);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test('image storage contracts reject incomplete adapters', () => {
assert.throws(
() => createImageStorageContract({ put() {} }),
/must implement get\(\)/,
);
});