85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
/**
|
|
* AC verification for Passo 6:
|
|
* storage.signPhotoUpload returns a valid URL; direct upload to MinIO works.
|
|
* storage.signPhotoDownload returns a valid URL; download matches uploaded content.
|
|
*/
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { config as loadEnv } from 'dotenv';
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
loadEnv({ path: path.resolve(here, '../.env') });
|
|
|
|
import { prisma } from '../packages/db/src/index.js';
|
|
import { appRouter, createTRPCContext } from '../packages/api/src/index.js';
|
|
import { createCallerFactory } from '../packages/api/src/trpc.js';
|
|
|
|
async function main() {
|
|
const adminUser = await prisma.user.findFirst({ where: { email: 'admin@demo.local' } });
|
|
if (!adminUser) throw new Error('Run pnpm db:seed first');
|
|
|
|
const ctx = await createTRPCContext({
|
|
user: {
|
|
id: adminUser.id,
|
|
email: adminUser.email,
|
|
role: adminUser.role as 'ADMIN',
|
|
tenantId: adminUser.tenantId,
|
|
},
|
|
headers: new Headers(),
|
|
});
|
|
|
|
const caller = createCallerFactory(appRouter)(ctx);
|
|
const content = 'fake-photo-content-for-router-smoke-test';
|
|
|
|
// signPhotoUpload
|
|
console.log('1. Calling storage.signPhotoUpload...');
|
|
const { uploadUrl, photoKey, expiresAt: uploadExpiry } = await caller.storage.signPhotoUpload({
|
|
contentType: 'image/jpeg',
|
|
byteSize: content.length,
|
|
});
|
|
console.log(` photoKey: ${photoKey}`);
|
|
console.log(` uploadUrl expires: ${uploadExpiry.toISOString()}`);
|
|
|
|
// Verify key format
|
|
if (!/^tenants\/.+\/maintenance\/.+\.jpg$/.test(photoKey)) {
|
|
throw new Error(`Unexpected photoKey format: ${photoKey}`);
|
|
}
|
|
console.log(' key format ✓');
|
|
|
|
// Upload directly to MinIO
|
|
console.log('2. Uploading via presigned PUT...');
|
|
const putRes = await fetch(uploadUrl, {
|
|
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 ✓');
|
|
|
|
// signPhotoDownload
|
|
console.log('3. Calling storage.signPhotoDownload...');
|
|
const { url: downloadUrl, expiresAt: downloadExpiry } = await caller.storage.signPhotoDownload({
|
|
photoKey,
|
|
});
|
|
console.log(` downloadUrl expires: ${downloadExpiry.toISOString()}`);
|
|
|
|
// Download and verify
|
|
console.log('4. Downloading and verifying...');
|
|
const getRes = await fetch(downloadUrl);
|
|
if (!getRes.ok) throw new Error(`GET failed: ${getRes.status}`);
|
|
const downloaded = await getRes.text();
|
|
if (downloaded !== content) {
|
|
throw new Error(`Content mismatch: expected "${content}", got "${downloaded}"`);
|
|
}
|
|
console.log(' Content matches ✓');
|
|
|
|
await prisma.$disconnect();
|
|
console.log('\nStorage router AC PASSED');
|
|
}
|
|
|
|
main().catch(async (err) => {
|
|
console.error('Storage router AC FAILED:', err);
|
|
await prisma.$disconnect();
|
|
process.exit(1);
|
|
});
|