// App-level helpers shared by the specs: localStorage seeding (Gemini key, // Postiz config, a restored session), the in-browser render seam, and a single // `openApp` that installs mocks, seeds storage and navigates. import { installApiMocks, JOB_ID, defaultClips, defaultResults } from './api-mocks.js'; // Mirrors SECRET_KEY in src/App.jsx (no VITE_ENCRYPTION_KEY is set by the build). const SECRET_KEY = 'Clipwright-Static-Salt-Change-Me'; export function encryptSecret(text) { let xor = ''; for (let i = 0; i < text.length; i += 1) { xor += String.fromCharCode(text.charCodeAt(i) ^ SECRET_KEY.charCodeAt(i % SECRET_KEY.length)); } return `ENC:${btoa(xor)}`; } export async function seedApp(page, { geminiKey = 'e2e-gemini-key', postizUrl, postizKey, session } = {}) { await page.addInitScript((payload) => { try { // A stored consent decision keeps the cookie banner out of the way // (the banner is otherwise shown to EEA timezones, which the CI // runner's TZ would make nondeterministic). window.localStorage.setItem('clipwright_consent', JSON.stringify({ v: 1, ts: new Date().toISOString(), necessary: true, analytics: true, marketing: false, })); if (payload.geminiKey) window.localStorage.setItem('gemini_key', payload.geminiKey); if (payload.postizUrl) window.localStorage.setItem('postiz_url', payload.postizUrl); if (payload.encryptedPostizKey) window.localStorage.setItem('postiz_key_v1', payload.encryptedPostizKey); if (payload.session) window.localStorage.setItem('clipwright_session', JSON.stringify(payload.session)); } catch (_) { /* storage unavailable */ } }, { geminiKey, postizUrl, encryptedPostizKey: postizKey ? encryptSecret(postizKey) : null, session, }); // Replace the WebCodecs/Remotion render with a deterministic blob URL so the // specs never depend on hardware encoders. See src/lib/renderInBrowser.js. await page.addInitScript(() => { window.__CW_E2E_RENDER__ = () => 'blob:http://127.0.0.1:4173/e2e-render'; }); } export function completedSession({ jobId = JOB_ID, clips = defaultClips() } = {}) { return { jobId, status: 'complete', results: { clips, cost_analysis: defaultResults().cost_analysis }, processingMedia: null, activeTab: 'dashboard', timestamp: Date.now(), }; } export async function openApp(page, { hash = '#app', seed, mocks } = {}) { const state = await installApiMocks(page, mocks); await seedApp(page, seed); await page.goto(`/${hash}`); return state; } export { installApiMocks, JOB_ID, defaultClips, defaultResults };