import { describe, expect, test } from 'bun:test' import { parseCookies, mergeCookies, formatCookies, normalizeCookiesInput, pasteCookies, } from './cookieImport.js' // User's exact prompt sample: extension JSON const EXTENSION_JSON_SAMPLE = `[ { "domain": "127.0.0.1", "expirationDate": 1794836374, "hostOnly": true, "httpOnly": true, "name": "session", "path": "/", "sameSite": "strict", "secure": false, "session": false, "storeId": null, "value": "sess_2242ce38c8c54a49b80fab32e8556b82" } ]` // User's exact prompt sample: Netscape format const NETSCAPE_SAMPLE = `# Netscape HTTP Cookie File # http://curl.haxx.se/rfc/cookie_spec.html # This file was generated by Cookie-Editor #HttpOnly_127.0.0.1 FALSE / FALSE 1794836374 session sess_2242ce38c8c54a49b80fab32e8556b82` // User's exact prompt sample: header string const HEADER_SAMPLE = 'session=sess_2242ce38c8c54a49b80fab32e8556b82' describe('parseCookies', () => { test('parses extension JSON array', () => { const res = parseCookies(EXTENSION_JSON_SAMPLE) expect(res.ok).toBe(true) expect(res.format).toBe('extension') expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses single extension cookie object', () => { const single = JSON.parse(EXTENSION_JSON_SAMPLE)[0] const res = parseCookies(JSON.stringify(single)) expect(res.ok).toBe(true) expect(res.format).toBe('extension') expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses Netscape cookie file with #HttpOnly_ and whitespace-separated fields', () => { const res = parseCookies(NETSCAPE_SAMPLE) expect(res.ok).toBe(true) expect(res.format).toBe('netscape') expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses standard tab-separated Netscape file', () => { const tabbed = [ '# Netscape HTTP Cookie File', '.example.com\tTRUE\t/\tFALSE\t1794836374\tsession\tsess_123', '#HttpOnly_.example.com\tTRUE\t/\tTRUE\t1794836374\tcsrf\ttoken_456', ].join('\n') const res = parseCookies(tabbed) expect(res.ok).toBe(true) expect(res.format).toBe('netscape') expect(res.cookies).toEqual([ ['session', 'sess_123'], ['csrf', 'token_456'], ]) }) test('handles CRLF line endings in Netscape file', () => { const crlf = NETSCAPE_SAMPLE.replace(/\n/g, '\r\n') const res = parseCookies(crlf) expect(res.ok).toBe(true) expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses header string', () => { const res = parseCookies(HEADER_SAMPLE) expect(res.ok).toBe(true) expect(res.format).toBe('header') expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses header string with Cookie: prefix and multiple pairs', () => { const res = parseCookies('Cookie: a=1; b=2=3; c="quoted"') expect(res.ok).toBe(true) expect(res.format).toBe('header') expect(res.cookies).toEqual([ ['a', '1'], ['b', '2=3'], // value preserves subsequent '=' ['c', '"quoted"'], ]) }) test('parses multiline header strings (newline separated)', () => { const res = parseCookies('a=1\nb=2\r\nc=3') expect(res.ok).toBe(true) expect(res.cookies).toEqual([['a', '1'], ['b', '2'], ['c', '3']]) }) test('identifies native archivr JSON', () => { const res = parseCookies('{"session": "sess_2242ce38c8c54a49b80fab32e8556b82"}') expect(res.ok).toBe(true) expect(res.format).toBe('archivr') expect(res.cookies).toEqual([ ['session', 'sess_2242ce38c8c54a49b80fab32e8556b82'], ]) }) test('parses empty object as native archivr JSON', () => { const res = parseCookies('{}') expect(res.ok).toBe(true) expect(res.format).toBe('archivr') expect(res.cookies).toEqual([]) }) test('rejects empty string', () => { expect(parseCookies('').ok).toBe(false) expect(parseCookies(' ').ok).toBe(false) }) test('rejects cookie name with space or semicolon', () => { expect(parseCookies('bad name=val').ok).toBe(false) expect(parseCookies('bad;name=val').ok).toBe(false) }) test('rejects cookie value with control chars', () => { expect(parseCookies('name=val\x00x').ok).toBe(false) }) test('rejects malformed extension entry missing value', () => { const res = parseCookies('[{"name": "foo"}]') expect(res.ok).toBe(false) expect(res.error).toMatch(/needs string "name" and "value"/) }) }) describe('mergeCookies', () => { test('merges base and incoming; incoming overwrites collision', () => { const base = [['a', '1'], ['b', '2']] const incoming = [['b', 'overwritten'], ['c', '3']] const res = mergeCookies(base, incoming) expect(res.cookies).toEqual({ a: '1', b: 'overwritten', c: '3', }) expect(res.added).toBe(1) expect(res.replaced).toBe(1) }) test('detects unchanged values as not replaced', () => { const base = [['a', '1']] const incoming = [['a', '1']] const res = mergeCookies(base, incoming) expect(res.added).toBe(0) expect(res.replaced).toBe(0) }) test('preserves reserved property names like __proto__', () => { const res = mergeCookies([], [['__proto__', 'safe']]) expect(Object.hasOwn(res.cookies, '__proto__')).toBe(true) expect(res.cookies['__proto__']).toBe('safe') }) }) describe('normalizeCookiesInput', () => { test('passes native JSON string through verbatim', () => { const native = '{\n "session": "abc"\n}' expect(normalizeCookiesInput(native)).toEqual({ ok: true, json: native }) }) test('converts extension JSON to pretty JSON', () => { const res = normalizeCookiesInput(EXTENSION_JSON_SAMPLE) expect(res.ok).toBe(true) expect(JSON.parse(res.json)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) }) test('converts Netscape to pretty JSON', () => { const res = normalizeCookiesInput(NETSCAPE_SAMPLE) expect(res.ok).toBe(true) expect(JSON.parse(res.json)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) }) test('converts header string to pretty JSON', () => { const res = normalizeCookiesInput(HEADER_SAMPLE) expect(res.ok).toBe(true) expect(JSON.parse(res.json)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) }) test('returns error for invalid input', () => { const res = normalizeCookiesInput('not a valid cookie string') expect(res.ok).toBe(false) }) }) describe('pasteCookies', () => { test('returns null if entire pasted result is already valid native JSON', () => { // Normal JSON typing/editing: pasting `"abc"` into `{"a": }` const current = '{"a": }' // Cursor right after the space: start = 6, end = 6 const res = pasteCookies(current, 6, 6, '"abc"') expect(res).toBeNull() }) test('returns null if clipboard is not cookies at all', () => { const res = pasteCookies('{}', 0, 2, 'hello world') expect(res).toBeNull() }) test('converts clipboard when textarea is default empty object and selected entirely', () => { const res = pasteCookies('{}', 0, 2, EXTENSION_JSON_SAMPLE) expect(res).not.toBeNull() expect(res.error).toBeUndefined() expect(JSON.parse(res.text)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) expect(res.message).toMatch(/Converted 1 cookie from extension JSON/) }) test('converts clipboard when textarea is completely empty', () => { const res = pasteCookies('', 0, 0, HEADER_SAMPLE) expect(res).not.toBeNull() expect(JSON.parse(res.text)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) expect(res.message).toMatch(/Converted 1 cookie from header string/) }) test('merges into existing native JSON', () => { const current = '{\n "existing": "val"\n}' const res = pasteCookies(current, current.length, current.length, NETSCAPE_SAMPLE) expect(res).not.toBeNull() expect(res.error).toBeUndefined() expect(JSON.parse(res.text)).toEqual({ existing: 'val', session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) expect(res.message).toMatch(/Merged 1 cookie from Netscape: 1 added, 0 replaced/) }) test('overwrites existing keys when pasting newer values', () => { const current = '{\n "session": "old_value"\n}' const res = pasteCookies(current, current.length, current.length, HEADER_SAMPLE) expect(res).not.toBeNull() expect(JSON.parse(res.text)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', }) expect(res.message).toMatch(/Merged 1 cookie from header string: 0 added, 1 replaced/) }) test('merges into existing Netscape content (if user already had that in textarea)', () => { const current = NETSCAPE_SAMPLE const clip = 'extra=value_123' const res = pasteCookies(current, current.length, current.length, clip) expect(res).not.toBeNull() expect(JSON.parse(res.text)).toEqual({ session: 'sess_2242ce38c8c54a49b80fab32e8556b82', extra: 'value_123', }) }) test('returns error when clipboard has cookies but remaining textarea is invalid', () => { const current = '{"broken json: ' const res = pasteCookies(current, current.length, current.length, HEADER_SAMPLE) expect(res).not.toBeNull() expect(res.error).toMatch(/existing content isn't valid cookies/) }) })