mirror your GitHub repos to tangled.org automatically
1

Configure Feed

Select the types of activity you want to include in your feed.

1import { afterEach, beforeEach, describe, expect, it } from 'vitest' 2import { readAccounts, sessionConfig } from '../../server/utils/server-session' 3 4const ORIGINAL_PASSWORD = process.env.NUXT_SESSION_PASSWORD 5const ORIGINAL_PUBLIC_URL = process.env.NUXT_PUBLIC_URL 6 7describe('server-session: sessionConfig', () => { 8 beforeEach(() => { 9 process.env.NUXT_SESSION_PASSWORD = 'a'.repeat(32) 10 process.env.NUXT_PUBLIC_URL = 'http://127.0.0.1:3000' 11 }) 12 13 afterEach(() => { 14 if (ORIGINAL_PASSWORD === undefined) delete process.env.NUXT_SESSION_PASSWORD 15 else process.env.NUXT_SESSION_PASSWORD = ORIGINAL_PASSWORD 16 if (ORIGINAL_PUBLIC_URL === undefined) delete process.env.NUXT_PUBLIC_URL 17 else process.env.NUXT_PUBLIC_URL = ORIGINAL_PUBLIC_URL 18 }) 19 20 it('throws if NUXT_SESSION_PASSWORD is missing', () => { 21 delete process.env.NUXT_SESSION_PASSWORD 22 expect(() => sessionConfig()).toThrow(/NUXT_SESSION_PASSWORD/) 23 }) 24 25 it('throws if NUXT_SESSION_PASSWORD is too short', () => { 26 process.env.NUXT_SESSION_PASSWORD = 'short' 27 expect(() => sessionConfig()).toThrow(/32\+ characters/) 28 }) 29 30 it('marks the cookie non-secure on 127.0.0.1 loopback dev', () => { 31 process.env.NUXT_PUBLIC_URL = 'http://127.0.0.1:3000' 32 const config = sessionConfig() 33 expect(config.cookie).toMatchObject({ secure: false, sameSite: 'lax', httpOnly: true, path: '/' }) 34 expect(config.name).toBe('synchub-session') 35 expect(config.maxAge).toBe(60 * 60 * 24 * 30) 36 }) 37 38 it('marks the cookie non-secure on localhost loopback dev', () => { 39 process.env.NUXT_PUBLIC_URL = 'http://localhost:3000' 40 const config = sessionConfig() 41 expect(config.cookie).toMatchObject({ secure: false }) 42 }) 43 44 it('marks the cookie secure when the deploy URL is not loopback', () => { 45 process.env.NUXT_PUBLIC_URL = 'https://synchub.to' 46 const config = sessionConfig() 47 expect(config.cookie).toMatchObject({ secure: true }) 48 }) 49}) 50 51const acct = (did: string, installationId: number) => ({ did, installationId }) 52 53describe('server-session: readAccounts', () => { 54 55 it('returns null for empty or malformed cookie data', () => { 56 expect(readAccounts({})).toBeNull() 57 expect(readAccounts({ active: 'did:plc:1', accounts: [] })).toBeNull() 58 expect(readAccounts({ active: 'did:plc:1' })).toBeNull() 59 // eslint-disable-next-line ts/no-unsafe-type-assertion 60 expect(readAccounts({ active: 'did:plc:1', accounts: 'nope' as unknown as [] })).toBeNull() 61 }) 62 63 it('keeps the active did when it names a present account', () => { 64 const result = readAccounts({ active: 'did:plc:2', accounts: [acct('did:plc:1', 1), acct('did:plc:2', 2)] }) 65 expect(result).toEqual({ active: 'did:plc:2', accounts: [acct('did:plc:1', 1), acct('did:plc:2', 2)] }) 66 }) 67 68 it('falls back active to the first account when active is stale', () => { 69 const result = readAccounts({ active: 'did:plc:gone', accounts: [acct('did:plc:1', 1), acct('did:plc:2', 2)] }) 70 expect(result?.active).toBe('did:plc:1') 71 }) 72 73 it('drops malformed account entries', () => { 74 const accounts = [ 75 acct('did:plc:1', 1), 76 // eslint-disable-next-line ts/no-unsafe-type-assertion 77 { did: 'did:plc:2' } as unknown as { did: string, installationId: number }, 78 // eslint-disable-next-line ts/no-unsafe-type-assertion 79 { installationId: 3 } as unknown as { did: string, installationId: number }, 80 ] 81 const result = readAccounts({ active: 'did:plc:1', accounts }) 82 expect(result?.accounts).toEqual([acct('did:plc:1', 1)]) 83 }) 84})