import { describe, test, expect } from 'vitest';
import { playwrightToNetscape } from './cookies.js';

describe('playwrightToNetscape', () => {
  test('starts with the Netscape header', () => {
    expect(playwrightToNetscape({ cookies: [] }).split('\n')[0]).toBe('# Netscape HTTP Cookie File');
  });

  test('converts a persistent cookie to a TSV line', () => {
    const out = playwrightToNetscape({
      cookies: [{ name: 'sessionid', value: 'abc', domain: '.instagram.com', path: '/', expires: 1900000000, secure: true }],
    });
    expect(out.trim().split('\n')[1]).toBe('.instagram.com\tTRUE\t/\tTRUE\t1900000000\tsessionid\tabc');
  });

  test('domain without leading dot is not flagged for subdomains', () => {
    const out = playwrightToNetscape({
      cookies: [{ name: 'x', value: 'y', domain: 'instagram.com', path: '/p', expires: 100, secure: false }],
    });
    expect(out.trim().split('\n')[1]).toBe('instagram.com\tFALSE\t/p\tFALSE\t100\tx\ty');
  });

  test('session cookie (expires -1) becomes expiry 0', () => {
    const out = playwrightToNetscape({
      cookies: [{ name: 'a', value: 'b', domain: 'instagram.com', path: '/', expires: -1, secure: false }],
    });
    expect(out.trim().split('\n')[1]!.split('\t')[4]).toBe('0');
  });
});
