|
| 1 | +import { describe, it, beforeEach, afterEach } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import { CookieStore } from '../../core/CookieStore.js'; |
| 4 | +import { mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { tmpdir } from 'node:os'; |
| 7 | + |
| 8 | +describe('CookieStore', () => { |
| 9 | + let store: CookieStore; |
| 10 | + let testDir: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + testDir = join(tmpdir(), `pardus-test-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`); |
| 14 | + mkdirSync(testDir, { recursive: true }); |
| 15 | + store = new CookieStore(testDir); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + if (existsSync(testDir)) { |
| 20 | + rmSync(testDir, { recursive: true, force: true }); |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + describe('saveCookies / loadCookies', () => { |
| 25 | + it('should save and load cookies round-trip', () => { |
| 26 | + const cookies = [ |
| 27 | + { name: 'session', value: 'abc123', domain: '.example.com', path: '/', sameSite: 'Lax' as const }, |
| 28 | + { name: 'token', value: 'xyz789', domain: '.example.com', path: '/api', secure: true, httpOnly: true }, |
| 29 | + ]; |
| 30 | + |
| 31 | + store.saveCookies('test-profile', cookies); |
| 32 | + |
| 33 | + const loaded = store.loadCookies('test-profile'); |
| 34 | + assert.strictEqual(loaded.length, 2); |
| 35 | + assert.strictEqual(loaded[0].name, 'session'); |
| 36 | + assert.strictEqual(loaded[0].value, 'abc123'); |
| 37 | + assert.strictEqual(loaded[0].domain, '.example.com'); |
| 38 | + assert.strictEqual(loaded[1].name, 'token'); |
| 39 | + assert.strictEqual(loaded[1].secure, true); |
| 40 | + assert.strictEqual(loaded[1].httpOnly, true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return empty array for non-existent profile', () => { |
| 44 | + const loaded = store.loadCookies('non-existent'); |
| 45 | + assert.deepStrictEqual(loaded, []); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return empty array for empty profile name', () => { |
| 49 | + const loaded = store.loadCookies(''); |
| 50 | + assert.deepStrictEqual(loaded, []); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should overwrite existing cookies on save', () => { |
| 54 | + store.saveCookies('test', [{ name: 'a', value: '1' }]); |
| 55 | + store.saveCookies('test', [{ name: 'b', value: '2' }, { name: 'c', value: '3' }]); |
| 56 | + |
| 57 | + const loaded = store.loadCookies('test'); |
| 58 | + assert.strictEqual(loaded.length, 2); |
| 59 | + assert.strictEqual(loaded[0].name, 'b'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should not save empty cookie array', () => { |
| 63 | + store.saveCookies('test', []); |
| 64 | + |
| 65 | + const loaded = store.loadCookies('test'); |
| 66 | + assert.deepStrictEqual(loaded, []); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not save when profile is empty', () => { |
| 70 | + store.saveCookies('', [{ name: 'a', value: '1' }]); |
| 71 | + |
| 72 | + const loaded = store.loadCookies(''); |
| 73 | + assert.deepStrictEqual(loaded, []); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should create the profile directory if it does not exist', () => { |
| 77 | + store.saveCookies('new-profile', [{ name: 'test', value: 'val' }]); |
| 78 | + |
| 79 | + const loaded = store.loadCookies('new-profile'); |
| 80 | + assert.strictEqual(loaded.length, 1); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('corrupt data handling', () => { |
| 85 | + it('should return empty array for corrupt JSON', () => { |
| 86 | + const profileDir = join(testDir, 'corrupt'); |
| 87 | + mkdirSync(profileDir, { recursive: true }); |
| 88 | + writeFileSync(join(profileDir, 'cookies.json'), 'not valid json{{{'); |
| 89 | + |
| 90 | + const loaded = store.loadCookies('corrupt'); |
| 91 | + assert.deepStrictEqual(loaded, []); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return empty array when cookies field is missing', () => { |
| 95 | + const profileDir = join(testDir, 'no-cookies'); |
| 96 | + mkdirSync(profileDir, { recursive: true }); |
| 97 | + writeFileSync(join(profileDir, 'cookies.json'), JSON.stringify({ savedAt: Date.now() })); |
| 98 | + |
| 99 | + const loaded = store.loadCookies('no-cookies'); |
| 100 | + assert.deepStrictEqual(loaded, []); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('deleteProfile', () => { |
| 105 | + it('should delete an existing profile', () => { |
| 106 | + store.saveCookies('to-delete', [{ name: 'a', value: '1' }]); |
| 107 | + |
| 108 | + assert.ok(store.loadCookies('to-delete').length > 0); |
| 109 | + store.deleteProfile('to-delete'); |
| 110 | + |
| 111 | + const loaded = store.loadCookies('to-delete'); |
| 112 | + assert.deepStrictEqual(loaded, []); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not throw for non-existent profile', () => { |
| 116 | + assert.doesNotThrow(() => store.deleteProfile('non-existent')); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('listProfiles', () => { |
| 121 | + it('should list profiles that have cookies', () => { |
| 122 | + store.saveCookies('profile-a', [{ name: 'a', value: '1' }]); |
| 123 | + store.saveCookies('profile-b', [{ name: 'b', value: '2' }]); |
| 124 | + |
| 125 | + const profiles = store.listProfiles(); |
| 126 | + assert.ok(profiles.includes('profile-a')); |
| 127 | + assert.ok(profiles.includes('profile-b')); |
| 128 | + assert.strictEqual(profiles.length, 2); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not list profiles with empty cookies (not saved)', () => { |
| 132 | + store.saveCookies('empty', []); |
| 133 | + |
| 134 | + const profiles = store.listProfiles(); |
| 135 | + assert.ok(!profiles.includes('empty')); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return empty array when no profiles exist', () => { |
| 139 | + const profiles = store.listProfiles(); |
| 140 | + assert.deepStrictEqual(profiles, []); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments