FieldOps/scripts/storage-smoke.ts
2026-05-16 15:32:56 +01:00

58 lines
1.9 KiB
TypeScript

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { config as loadEnv } from 'dotenv';
// Load .env from repo root before importing storage (env vars must be set first).
const here = path.dirname(fileURLToPath(import.meta.url));
loadEnv({ path: path.resolve(here, '../.env') });
import { makeStorage } from '../packages/storage/src/index.js';
async function main() {
const storage = makeStorage();
const testKey = `test/smoke-${Date.now()}.jpg`;
const content = 'fake-jpeg-content-for-smoke-test';
console.log('1. Generating presigned PUT URL...');
const { url: putUrl, expiresAt: putExpiry } = await storage.signPut(
testKey,
'image/jpeg',
content.length,
300,
);
console.log(` PUT URL (expires ${putExpiry.toISOString()}): ${putUrl.slice(0, 80)}...`);
console.log('2. Uploading test content via presigned PUT...');
const putRes = await fetch(putUrl, {
method: 'PUT',
body: content,
headers: { 'Content-Type': 'image/jpeg' },
});
if (!putRes.ok) {
throw new Error(`PUT failed: ${putRes.status} ${await putRes.text()}`);
}
console.log(' Upload OK');
console.log('3. Generating presigned GET URL...');
const { url: getUrl, expiresAt: getExpiry } = await storage.signGet(testKey, 60);
console.log(` GET URL (expires ${getExpiry.toISOString()}): ${getUrl.slice(0, 80)}...`);
console.log('4. Downloading and verifying content...');
const getRes = await fetch(getUrl);
if (!getRes.ok) {
throw new Error(`GET failed: ${getRes.status} ${await getRes.text()}`);
}
const downloaded = await getRes.text();
if (downloaded !== content) {
throw new Error(`Content mismatch!\n expected: "${content}"\n got: "${downloaded}"`);
}
console.log(' Download OK — content matches');
console.log('\nSmoke test PASSED');
}
main().catch((err) => {
console.error('Smoke test FAILED:', err);
process.exit(1);
});