import test from 'node:test'; import assert from 'node:assert/strict'; import { toCypressSnippet, toSeleniumPythonDict, toWebdriverIOSnippet, exportSessionAs, type StorageState } from './session-export.js'; const sampleState: StorageState = { cookies: [ { name: 'session_id', value: 'abc123', domain: '.example.com', path: '/', secure: true, httpOnly: true, sameSite: 'Lax', expires: 1893456000 }, ], origins: [ { origin: 'https://example.com', localStorage: [{ name: 'authToken', value: 'xyz789' }] }, ], }; test('toCypressSnippet: emits cy.setCookie for each cookie and cy.window().localStorage for each origin', () => { const out = toCypressSnippet(sampleState); assert.match(out, /cy\.setCookie\("session_id", "abc123"/); assert.match(out, /domain: "\.example\.com"/); assert.match(out, /secure: true/); assert.match(out, /httpOnly: true/); assert.match(out, /cy\.visit\("https:\/\/example\.com"\)/); assert.match(out, /localStorage\.setItem\("authToken", "xyz789"\)/); assert.match(out, /Cypress\.Commands\.add\('restoreZetaSession'/); }); test('toSeleniumPythonDict: navigates to each cookie domain before add_cookie, and to each origin for localStorage', () => { const out = toSeleniumPythonDict(sampleState); assert.match(out, /driver\.get\("https:\/\/example\.com"\)/); assert.match(out, /driver\.add_cookie\(/); assert.match(out, /"name": "session_id"/); assert.match(out, /"value": "abc123"/); assert.match(out, /driver\.execute_script\(/); }); test('toSeleniumPythonDict: emits Python True/False, not JSON true/false, for boolean fields', () => { const out = toSeleniumPythonDict(sampleState); // sampleState's cookie has secure: true — must render as Python's True. assert.match(out, /"secure": True/); assert.doesNotMatch(out, /:\s*true\b/); assert.doesNotMatch(out, /:\s*false\b/); }); test('toWebdriverIOSnippet: browser.setCookies() array shape close to Playwright cookies, plus localStorage via execute()', () => { const out = toWebdriverIOSnippet(sampleState); assert.match(out, /browser\.setCookies\(/); assert.match(out, /"name": "session_id"/); assert.match(out, /"domain": "\.example\.com"/); assert.match(out, /browser\.url\("https:\/\/example\.com"\)/); assert.match(out, /localStorage\.setItem\("authToken", "xyz789"\)/); assert.match(out, /module\.exports = \{ restoreZetaSession \}/); }); test('exportSessionAs: returns correct filename/mimeType per format', () => { const cy = exportSessionAs('cypress', sampleState); assert.equal(cy.filename, 'zeta-session.cy-commands.js'); assert.equal(cy.mimeType, 'application/javascript'); const py = exportSessionAs('selenium-python', sampleState); assert.equal(py.filename, 'zeta_session.py'); assert.equal(py.mimeType, 'text/x-python'); const wdio = exportSessionAs('webdriverio', sampleState); assert.equal(wdio.filename, 'zeta-session.wdio.js'); assert.equal(wdio.mimeType, 'application/javascript'); }); test('exportSessionAs: throws on an unknown format', () => { assert.throws(() => exportSessionAs('unknown' as any, sampleState), /Unknown session export format/); }); test('handles empty cookies/origins gracefully (no crash, minimal output)', () => { const empty: StorageState = { cookies: [], origins: [] }; assert.doesNotThrow(() => toCypressSnippet(empty)); assert.doesNotThrow(() => toSeleniumPythonDict(empty)); assert.doesNotThrow(() => toWebdriverIOSnippet(empty)); });