UNPKG

@prodbirdy/mockup-generator

Version:

Serverless-optimized TypeScript SDK for generating high-quality product mockups from PSD templates

128 lines (107 loc) โ€ข 3.98 kB
#!/usr/bin/env bun import chalk from "chalk"; import { R2Client, generateUniqueFileName, createR2ClientFromEnv, } from "./r2-helpers"; import { printError, printInfo, printSuccess } from "./formatting"; import type { R2Config } from "../constants"; async function testR2Upload() { console.log(chalk.cyan("\n๐Ÿงช Testing R2 Upload Integration\n")); // Try to load from environment variables first let client = createR2ClientFromEnv(); if (!client) { // If no env vars, use test configuration console.log(chalk.yellow("โš ๏ธ No R2 environment variables found")); console.log(chalk.gray("Set these environment variables:")); console.log(chalk.gray(" R2_ENDPOINT")); console.log(chalk.gray(" R2_ACCESS_KEY_ID")); console.log(chalk.gray(" R2_SECRET_ACCESS_KEY")); console.log(chalk.gray(" R2_BUCKET")); console.log(); // Example configuration for testing const testConfig: R2Config = { endpoint: "https://14ee215fd1c82dcadf401ceed97d9f2c.r2.cloudflarestorage.com", accessKeyId: "452c13d963f19e3b7469cdf73aece521", secretAccessKey: "0799a5729f7af9857b0ee60481857105d9002343b57b392518deca8585f8f2c5", bucket: "public", }; console.log( chalk.gray("Using example configuration (update with your credentials)") ); client = new R2Client(testConfig); } try { // Test 1: Upload a text file printInfo("Test 1: Uploading text file..."); const textContent = "Hello from R2 upload test!"; const textFileName = generateUniqueFileName("test", "txt"); const textResult = await client.upload( { key: `tests/${textFileName}`, body: textContent, contentType: "text/plain", }, true ); printSuccess(`โœ“ Text file uploaded: ${textResult.url}`); // Test 2: Upload a mock PNG file printInfo("\nTest 2: Uploading mock PNG file..."); const mockPngBuffer = Buffer.alloc(1024 * 10); // 10KB mock PNG mockPngBuffer.fill(0x89); // PNG magic number const pngFileName = generateUniqueFileName("test-image", "png"); const pngResult = await client.uploadImage( mockPngBuffer, pngFileName, "png", "tests/images", true ); printSuccess(`โœ“ PNG file uploaded: ${pngResult.url}`); printInfo(` Bucket: ${pngResult.bucket}`); printInfo(` Key: ${pngResult.key}`); // Test 3: Upload a mock PSD file printInfo("\nTest 3: Uploading mock PSD file..."); const mockPsdBuffer = Buffer.alloc(1024 * 100); // 100KB mock PSD mockPsdBuffer.write("8BPS"); // PSD magic number const psdFileName = generateUniqueFileName("test-mockup", "psd"); const psdResult = await client.uploadPSD( mockPsdBuffer, psdFileName, "tests/psds", true ); printSuccess(`โœ“ PSD file uploaded: ${psdResult.url}`); // Test 4: Download the text file printInfo("\nTest 4: Downloading uploaded text file..."); const downloadedBuffer = await client.download( `tests/${textFileName}`, true ); const downloadedText = downloadedBuffer.toString(); if (downloadedText === textContent) { printSuccess("โœ“ Downloaded content matches uploaded content"); } else { printError("โœ— Downloaded content does not match!"); } // Test 5: Generate signed URL printInfo("\nTest 5: Generating signed URL..."); const signedUrl = await client.getSignedUrl(`tests/${textFileName}`, 3600); printSuccess(`โœ“ Signed URL generated (expires in 1 hour):`); console.log(chalk.gray(` ${signedUrl.substring(0, 80)}...`)); console.log(chalk.green.bold("\n๐ŸŽ‰ All R2 upload tests passed!\n")); } catch (error) { printError("Test failed:", error as Error, true); process.exit(1); } } // Run tests if called directly if (import.meta.main) { testR2Upload().catch((error) => { console.error(chalk.red("Unexpected error:"), error); process.exit(1); }); }