diff --git a/scripts/agent/smoke-test.mjs b/scripts/agent/smoke-test.mjs deleted file mode 100644 index 83d4fc95..00000000 --- a/scripts/agent/smoke-test.mjs +++ /dev/null @@ -1,271 +0,0 @@ - -import './setup-test-env.js'; -import { startRelay } from './load-test-relay.mjs'; -import { NostrClient } from '../../js/nostr/client.js'; -import { decryptDM } from '../../js/dmDecryptor.js'; -import { - buildVideoPostEvent, - buildLegacyDirectMessageEvent -} from '../../js/nostrEventSchemas.js'; -import { generateSecretKey, getPublicKey } from 'nostr-tools'; -import { chromium } from 'playwright'; -import { spawn } from 'child_process'; -import fs from 'fs'; -import path from 'path'; - -const RELAY_PORT = 8899; -const RELAY_URL = `ws://localhost:${RELAY_PORT}`; -const HTTP_PORT = 8000; -const HTTP_URL = `http://localhost:${HTTP_PORT}`; - -// Artifacts -const ARTIFACTS_DIR = 'artifacts'; -const LOG_FILE = path.join(ARTIFACTS_DIR, `smoke-${new Date().toISOString().split('T')[0].replace(/-/g, '')}.log`); -const SCREENSHOT_FILE = path.join(ARTIFACTS_DIR, 'smoke-test-ui.png'); - -// Logger -function log(message) { - const timestamp = new Date().toISOString(); - const line = `[${timestamp}] ${message}`; - console.log(line); - fs.appendFileSync(LOG_FILE, line + '\n'); -} - -// Generate Keys -const bytesToHex = (bytes) => Buffer.from(bytes).toString('hex'); -const EPHEMERAL_SK_BYTES = generateSecretKey(); -const EPHEMERAL_SK = bytesToHex(EPHEMERAL_SK_BYTES); -const EPHEMERAL_PK = getPublicKey(EPHEMERAL_SK_BYTES); - -async function startHttpServer() { - log('Starting HTTP Server...'); - const serve = spawn('npx', ['serve', '.', '-p', String(HTTP_PORT)], { - stdio: 'ignore', - shell: true - }); - - const maxRetries = 20; - for (let i = 0; i < maxRetries; i++) { - try { - await fetch(HTTP_URL); - log('HTTP Server is ready.'); - return serve; - } catch (e) { - await new Promise(r => setTimeout(r, 250)); - } - } - serve.kill(); - throw new Error('HTTP Server failed to start within timeout.'); -} - -async function runSmokeTest() { - // Ensure artifacts dir - if (!fs.existsSync(ARTIFACTS_DIR)) { - fs.mkdirSync(ARTIFACTS_DIR); - } - fs.writeFileSync(LOG_FILE, ''); // Clear log - - let relayServer; - let httpServer; - let browser; - let nodeClient; - let exitCode = 0; - - try { - log('--- Smoke Test Started ---'); - log(`Ephemeral Pubkey: ${EPHEMERAL_PK}`); - - // 1. Start Infrastructure - relayServer = startRelay(RELAY_PORT); - httpServer = await startHttpServer(); - - // 2. Initialize Node Client - log('Initializing Node Client...'); - nodeClient = new NostrClient(); - nodeClient.relays = [RELAY_URL]; - nodeClient.writeRelays = [RELAY_URL]; - nodeClient.readRelays = [RELAY_URL]; - - await nodeClient.registerPrivateKeySigner({ privateKey: EPHEMERAL_SK }); - await nodeClient.init(); - log('Node Client connected.'); - - // 3. Publish Video Event (Node) - log('Node: Publishing Video Post...'); - const videoEventTemplate = buildVideoPostEvent({ - pubkey: EPHEMERAL_PK, - created_at: Math.floor(Date.now() / 1000), - dTagValue: `smoke-test-${Date.now()}`, - content: { - version: 3, - title: 'Smoke Test Video', - url: 'https://example.com/video.mp4', - description: 'A video to verify feed rendering.', - mode: 'live', - videoRootId: `smoke-root-${Date.now()}` - } - }); - - const { signedEvent: publishedVideo } = await nodeClient.signAndPublishEvent(videoEventTemplate, { - context: 'smoke-video' - }); - log(`Node: Published Video ID: ${publishedVideo.id}`); - - // 4. Publish DM (Node -> Self) - log('Node: Publishing Encrypted DM...'); - const dmMessage = "Smoke Test Secret Message"; - const signer = await nodeClient.ensureActiveSignerForPubkey(EPHEMERAL_PK); - const ciphertext = await signer.nip04Encrypt(EPHEMERAL_PK, dmMessage); - - const dmTemplate = buildLegacyDirectMessageEvent({ - pubkey: EPHEMERAL_PK, - created_at: Math.floor(Date.now() / 1000), - recipientPubkey: EPHEMERAL_PK, - ciphertext: ciphertext - }); - - const { signedEvent: publishedDM } = await nodeClient.signAndPublishEvent(dmTemplate, { - context: 'smoke-dm' - }); - log(`Node: Published DM ID: ${publishedDM.id}`); - - // 5. Browser Test (Playwright) - log('Browser: Launching...'); - browser = await chromium.launch({ headless: true }); - const context = await browser.newContext(); - - // Inject LocalStorage for Auto-Login and Relay Config - const savedProfiles = { - entries: [{ pubkey: EPHEMERAL_PK, authType: "nip07" }], - activePubkey: EPHEMERAL_PK, - version: 1 - }; - const relayList = [{ url: RELAY_URL, mode: "both" }]; - - // Note: Key format derived from investigation: bitvid:profile::relayList:v1 - const relayStorageKey = `bitvid:profile:${EPHEMERAL_PK}:relayList:v1`; - - await context.addInitScript(({ profiles, relays, relayKey }) => { - // Disable Whitelist Mode to allow fresh pubkey login - window.localStorage.setItem('bitvid_admin_whitelist_mode', 'false'); - - window.localStorage.setItem('bitvid:savedProfiles:v1', JSON.stringify(profiles)); - window.localStorage.setItem(relayKey, JSON.stringify(relays)); - // Mock NIP-07 to avoid errors if app tries to use it (though we won't sign in browser) - window.nostr = { - getPublicKey: async () => profiles.activePubkey, - signEvent: async () => { throw new Error("Mock NIP-07 cannot sign"); } - }; - }, { profiles: savedProfiles, relays: relayList, relayKey: relayStorageKey }); - - const page = await context.newPage(); - - log(`Browser: Navigating to ${HTTP_URL}...`); - - // Capture Console - page.on('console', msg => log(`[Browser Console] ${msg.type()}: ${msg.text()}`)); - page.on('pageerror', err => log(`[Browser Error] ${err.message}`)); - - await page.goto(HTTP_URL); - - // Verify Login (Profile Button presence) - log('Browser: Verifying Login...'); - // #profileButton might be hidden initially, wait for it - try { - await page.waitForSelector('#profileButton', { state: 'visible', timeout: 15000 }); - log('Browser: Login verified (Profile button visible).'); - } catch (e) { - log('Browser: Login verification failed (Profile button not found). Dumping localStorage...'); - const storage = await page.evaluate(() => JSON.stringify(localStorage)); - log(`[Browser Storage] ${storage}`); - await page.screenshot({ path: path.join(ARTIFACTS_DIR, 'smoke-fail-login.png') }); - } - - // Verify Feed (Video Card presence) - log('Browser: Verifying Video in Feed...'); - // Look for the video title - try { - // Wait longer for feed to load (relays might be slow) - await page.waitForSelector(`text=Smoke Test Video`, { timeout: 60000 }); - log('Browser: Video found in feed!'); - } catch (e) { - log('Browser: Video NOT found in feed. Taking screenshot...'); - await page.screenshot({ path: path.join(ARTIFACTS_DIR, 'smoke-fail-feed.png') }); - - // Debug: Check relays - const relayState = await page.evaluate(() => { - // Try to access global nostrClient if exposed, or check localStorage again - return { - localStorage: JSON.stringify(localStorage), - // If we can't access client directly, we rely on logs - }; - }); - log(`[Browser Debug] Storage: ${relayState.localStorage}`); - - throw new Error('Browser: Video NOT found in feed.'); - } - - // Screenshot - await page.screenshot({ path: SCREENSHOT_FILE }); - log(`Browser: Screenshot saved to ${SCREENSHOT_FILE}`); - - // 6. Node Verification (DM Decrypt) - log('Node: Verifying DM Decryption...'); - // Fetch back - await new Promise(r => setTimeout(r, 500)); - const fetchedDM = await nodeClient.fetchRawEventById(publishedDM.id); - if (!fetchedDM) { - throw new Error('Node: Failed to fetch DM back.'); - } - - const decryptContext = await nodeClient.buildDmDecryptContext(EPHEMERAL_PK); - const decryptionResult = await decryptDM(fetchedDM, decryptContext); - - if (!decryptionResult.ok) { - log(`Node: Decryption failed: ${JSON.stringify(decryptionResult.errors)}`); - throw new Error('Node: Decryption failed.'); - } - - if (decryptionResult.plaintext !== dmMessage) { - throw new Error(`Node: Decryption mismatch. Got "${decryptionResult.plaintext}"`); - } - log(`Node: DM Decrypted successfully: "${decryptionResult.plaintext}"`); - - log('--- Smoke Test PASSED ---'); - - // Generate JSON Summary - const summary = { - timestamp: new Date().toISOString(), - status: "success", - details: { - videoEventId: publishedVideo.id, - dmEventId: publishedDM.id, - ephemeralPubkey: EPHEMERAL_PK - } - }; - const summaryFile = path.join(ARTIFACTS_DIR, `smoke-summary-${new Date().toISOString().split('T')[0].replace(/-/g, '')}.json`); - fs.writeFileSync(summaryFile, JSON.stringify(summary, null, 2)); - log(`Summary saved to ${summaryFile}`); - - } catch (err) { - log(`--- Smoke Test FAILED: ${err.message} ---`); - if (err.stack) log(err.stack); - exitCode = 1; - } finally { - // Cleanup - log('Cleaning up...'); - if (nodeClient && nodeClient.pool) { - // Close pool connections - if (typeof nodeClient.pool.close === 'function') { - nodeClient.pool.close(nodeClient.relays); - } - } - if (browser) await browser.close(); - if (httpServer) httpServer.kill(); - if (relayServer) await relayServer.close(); - - process.exit(exitCode); - } -} - -runSmokeTest(); diff --git a/tests/smoke/smoke-test.mjs b/tests/smoke/smoke-test.mjs new file mode 100644 index 00000000..880566d0 --- /dev/null +++ b/tests/smoke/smoke-test.mjs @@ -0,0 +1,196 @@ +import { chromium } from 'playwright'; +import { spawn } from 'child_process'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const PORT = 8000; +const URL = `http://localhost:${PORT}`; +const ARTIFACTS_DIR = path.resolve('artifacts'); +const LOG_FILE = path.join(ARTIFACTS_DIR, `smoke-${new Date().toISOString().split('T')[0].replace(/-/g, '')}.log`); +const SUMMARY_FILE = path.join(ARTIFACTS_DIR, 'smoke-summary.json'); + +if (!fs.existsSync(ARTIFACTS_DIR)) { + fs.mkdirSync(ARTIFACTS_DIR); +} + +function log(msg) { + const timestamp = new Date().toISOString(); + const line = `[${timestamp}] ${msg}`; + console.log(line); + try { + fs.appendFileSync(LOG_FILE, line + '\n'); + } catch (e) { + // Ignore log write errors + } +} + +async function main() { + log('Starting smoke test...'); + let serverProcess; + let browser; + + try { + // Start Python Server + log('Starting local server (python3 -m http.server)...'); + serverProcess = spawn('python3', ['-m', 'http.server', String(PORT)], { + stdio: 'ignore' + }); + + // Give it time to start + await new Promise(r => setTimeout(r, 2000)); + + // Launch Browser + log('Launching browser...'); + browser = await chromium.launch({ headless: true }); + const context = await browser.newContext(); + const page = await context.newPage(); + + // Capture console logs from page + page.on('console', msg => { + if (msg.type() === 'error') { + log(`[BROWSER ERROR] ${msg.text()}`); + } else { + // log(`[BROWSER] ${msg.text()}`); // verbose + } + }); + page.on('pageerror', err => log(`[BROWSER EXCEPTION] ${err}`)); + + log(`Navigating to ${URL}...`); + await page.goto(URL); + await page.waitForLoadState('networkidle'); + log('Page loaded.'); + + // Inject Test Logic + log('Running in-browser smoke test...'); + + const result = await page.evaluate(async () => { + const logs = []; + function browserLog(msg) { + console.log(msg); + logs.push(msg); + } + + try { + browserLog('Step 1: Importing Client...'); + // Note: In browser context, paths are relative to the served root (http://localhost:8000/) + const { nostrClient } = await import('./js/nostrClientFacade.js'); + const { encodeHexToNpub } = await import('./js/nostr/nip46Client.js'); + + if (!nostrClient) throw new Error('nostrClient not found'); + browserLog('nostrClient imported.'); + + browserLog('Step 2: Generating Key & Login...'); + const randomBytes = new Uint8Array(32); + window.crypto.getRandomValues(randomBytes); + const privateKey = Array.from(randomBytes) + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); + + const { pubkey } = await nostrClient.registerPrivateKeySigner({ privateKey, persist: false }); + nostrClient.pubkey = pubkey; // Explicitly set it on the client instance + + if (!nostrClient.pubkey) throw new Error('Login failed: pubkey is null'); + browserLog(`Logged in as: ${nostrClient.pubkey}`); + + browserLog('Step 3: Connecting to Relays...'); + const connectResults = await nostrClient.connectToRelays(); + const successful = connectResults.filter(r => r.success); + browserLog(`Connected to ${successful.length} relays.`); + + if (successful.length === 0) { + browserLog('Warning: Failed to connect to any relay. Tests depending on network will likely fail or use local cache.'); + } + + browserLog('Step 4: Publishing Video...'); + const seriesId = `smoke-test-${Date.now()}-${Math.floor(Math.random()*1000)}`; + + const videoPayload = { + title: "Smoke Test Video " + Date.now(), + description: "Automated smoke test artifact.", + url: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", + videoRootId: seriesId, + mimeType: "video/mp4" + }; + + const videoEvent = await nostrClient.publishVideo(videoPayload, nostrClient.pubkey); + browserLog(`Video published. Event ID: ${videoEvent.id}`); + + browserLog('Step 5: Verifying Video Presence...'); + const fetched = await nostrClient.getEventById(videoEvent.id); + if (!fetched) throw new Error('Failed to retrieve video event from client cache/network.'); + browserLog('Video event verified.'); + + browserLog('Step 6: DM Flow (Send to Self)...'); + const message = "Smoke test DM " + Date.now(); + const npub = encodeHexToNpub(nostrClient.pubkey); + browserLog(`Sending DM to self (${npub})...`); + + const dmResult = await nostrClient.sendDirectMessage(npub, message); + if (!dmResult.ok) throw new Error(`DM send failed: ${dmResult.error} ${JSON.stringify(dmResult.details || {})}`); + browserLog('DM sent.'); + + browserLog('Step 7: Verifying DM Decryption...'); + let found = null; + for (let i = 0; i < 5; i++) { + await new Promise(r => setTimeout(r, 1000)); + const dms = await nostrClient.listDirectMessages(nostrClient.pubkey, { limit: 10 }); + found = dms.find(dm => dm.plaintext === message); + if (found) break; + } + + if (!found) { + throw new Error('DM verification failed: Message not found in decrypted list after retries.'); + } + + browserLog('DM verified and decrypted successfully.'); + + return { success: true, logs }; + } catch (err) { + browserLog(`Error: ${err.message}`); + return { success: false, error: err.message, stack: err.stack, logs }; + } + }); + + if (result.success) { + log('SUCCESS: All smoke tests passed.'); + fs.writeFileSync(SUMMARY_FILE, JSON.stringify({ + status: 'success', + timestamp: new Date().toISOString(), + logs: result.logs + }, null, 2)); + } else { + log('FAILURE: Smoke tests failed.'); + log(`Error: ${result.error}`); + fs.writeFileSync(SUMMARY_FILE, JSON.stringify({ + status: 'failure', + timestamp: new Date().toISOString(), + error: result.error, + stack: result.stack, + logs: result.logs + }, null, 2)); + + const screenshotPath = path.join(ARTIFACTS_DIR, 'smoke-failure.png'); + await page.screenshot({ path: screenshotPath }); + log(`Screenshot saved to ${screenshotPath}`); + + process.exitCode = 1; + } + + } catch (error) { + log(`CRITICAL ERROR: ${error.message}`); + console.error(error); + process.exitCode = 1; + } finally { + if (browser) await browser.close(); + if (serverProcess) { + log('Stopping server...'); + serverProcess.kill(); + } + } +} + +main();