/**
 * SET 29c — notification authentication + invocation logging.
 * Pure formatters for both notification paths (page / SW); see web/src/notifyLog.ts.
 */
import { describe, expect, it } from 'vitest';
import { formatPageNotifyLog, formatSwNotifyLog } from '../web/src/notifyLog';

describe('SET 29c formatPageNotifyLog', () => {
  it('formats a fired notification', () => {
    expect(formatPageNotifyLog('granted', true, { kind: 'fired' }))
      .toBe('notify(page): perm=granted hidden=true → fired');
  });

  it('formats the hidden-tab gate skip so it reads as by-design, not breakage', () => {
    expect(formatPageNotifyLog('granted', false, { kind: 'skipped', reason: 'tab visible' }))
      .toBe('notify(page): perm=granted hidden=false → skipped(tab visible)');
  });

  it('formats a permission-denied skip', () => {
    expect(formatPageNotifyLog('denied', true, { kind: 'skipped', reason: 'permission denied' }))
      .toBe('notify(page): perm=denied hidden=true → skipped(permission denied)');
  });

  it('formats default (never asked) permission', () => {
    expect(formatPageNotifyLog('default', true, { kind: 'skipped', reason: 'permission default' }))
      .toBe('notify(page): perm=default hidden=true → skipped(permission default)');
  });

  it('formats an unsupported environment', () => {
    expect(formatPageNotifyLog('unsupported', false, { kind: 'skipped', reason: 'unsupported' }))
      .toBe('notify(page): perm=unsupported hidden=false → skipped(unsupported)');
  });

  it('formats a thrown error', () => {
    expect(formatPageNotifyLog('granted', true, { kind: 'error', message: 'boom' }))
      .toBe('notify(page): perm=granted hidden=true → error(boom)');
  });
});

describe('SET 29c formatSwNotifyLog', () => {
  it('formats a created notification id', () => {
    expect(formatSwNotifyLog({ id: 'abc-123' })).toBe('notify(sw): created id=abc-123');
  });

  it('formats a missing id as empty string, not "undefined"', () => {
    expect(formatSwNotifyLog({})).toBe('notify(sw): created id=');
  });

  it('formats a failure — the empty-catch case this set fixes', () => {
    expect(formatSwNotifyLog({ error: 'notifications permission blocked by the OS' }))
      .toBe('notify(sw): FAILED notifications permission blocked by the OS');
  });

  it('prefers the error branch when both id and error are present', () => {
    expect(formatSwNotifyLog({ id: 'x', error: 'boom' })).toBe('notify(sw): FAILED boom');
  });
});
