#!/usr/bin/env tsx import { getScreenshotConfig, validateScreenshotPath, takeScreenshot, getLastScreenshot, imageToBase64, } from "./src/screenshot.js"; import { mkdirSync, rmSync, existsSync } from "fs"; import { join } from "path"; async function runTests() { console.log("๐Ÿงช Running snap-happy tests...\n"); // Test 1: Environment variable parsing console.log("Test 1: Environment variable parsing"); try { // Test with missing env var delete process.env.SNAP_HAPPY_SCREENSHOT_PATH; delete process.env.MCP_SERVER_NAME; try { getScreenshotConfig(); console.log("โŒ Should have thrown error for missing env var"); } catch (error) { console.log("โœ… Correctly throws error for missing env var:", (error as Error).message); } // Test with env var set process.env.SNAP_HAPPY_SCREENSHOT_PATH = "/tmp/snap-happy-test"; const config = getScreenshotConfig(); console.log("โœ… Environment variable parsing works:", config.screenshotPath); } catch (error) { console.log("โŒ Environment variable test failed:", (error as Error).message); } // Test 2: Directory validation and creation console.log("\nTest 2: Directory validation and creation"); try { const testDir = "/tmp/snap-happy-test"; // Clean up if exists if (existsSync(testDir)) { rmSync(testDir, { recursive: true }); } validateScreenshotPath(testDir); console.log("โœ… Directory created and validated:", testDir); // Test writable check validateScreenshotPath(testDir); console.log("โœ… Directory write validation works"); } catch (error) { console.log("โŒ Directory validation test failed:", (error as Error).message); } // Test 3: Screenshot functionality (only on macOS) console.log("\nTest 3: Screenshot functionality"); try { const testDir = "/tmp/snap-happy-test"; if (process.platform === "darwin") { console.log("๐Ÿ“ธ Taking test screenshot..."); const screenshotPath = takeScreenshot(testDir); console.log("โœ… Screenshot taken:", screenshotPath); // Test getting last screenshot const lastScreenshot = getLastScreenshot(testDir); console.log("โœ… Last screenshot found:", lastScreenshot); // Test base64 conversion if (lastScreenshot) { const base64Data = imageToBase64(lastScreenshot); console.log("โœ… Base64 conversion works, length:", base64Data.length); // Verify it's valid base64 PNG if (base64Data.length > 0 && base64Data.match(/^[A-Za-z0-9+/]*={0,2}$/)) { console.log("โœ… Valid base64 format"); } else { console.log("โŒ Invalid base64 format"); } } } else { console.log("โญ๏ธ Skipping screenshot test on non-macOS platform"); } } catch (error) { console.log("โŒ Screenshot test failed:", (error as Error).message); } // Test 4: Error handling console.log("\nTest 4: Error handling"); try { // Test invalid directory try { getLastScreenshot("/nonexistent/directory"); console.log("โŒ Should have thrown error for invalid directory"); } catch (error) { console.log("โœ… Correctly handles invalid directory:", (error as Error).message); } // Test invalid image file try { imageToBase64("/nonexistent/file.png"); console.log("โŒ Should have thrown error for missing file"); } catch (error) { console.log("โœ… Correctly handles missing image file:", (error as Error).message); } } catch (error) { console.log("โŒ Error handling test failed:", (error as Error).message); } console.log("\n๐ŸŽ‰ Tests completed!"); // Cleanup try { if (existsSync("/tmp/snap-happy-test")) { rmSync("/tmp/snap-happy-test", { recursive: true }); console.log("๐Ÿงน Test directory cleaned up"); } } catch (error) { console.log("โš ๏ธ Cleanup warning:", (error as Error).message); } } runTests().catch(console.error);