import { describe, it, expect } from 'vitest';
import { parseMe } from './me.adapter';

describe('parseMe (trust boundary)', () => {
  it('parses a known capability list', () => {
    const r = parseMe({ capabilities: ['brief:approve', 'brief:reject', 'brief:send'] });
    expect(r).toEqual({ ok: true, value: ['brief:approve', 'brief:reject', 'brief:send'] });
  });

  it('parses an empty list (drafter — no capabilities)', () => {
    expect(parseMe({ capabilities: [] })).toEqual({ ok: true, value: [] });
  });

  it('recognizes the admin org-template capability (WP-23)', () => {
    expect(parseMe({ capabilities: ['orgtemplate:edit'] })).toEqual({
      ok: true,
      value: ['orgtemplate:edit'],
    });
  });

  it('drops unrecognized capability strings instead of rejecting the response', () => {
    const r = parseMe({ capabilities: ['brief:approve', 'unknown:future-thing'] });
    expect(r).toEqual({ ok: true, value: ['brief:approve'] });
  });

  it('rejects malformed responses instead of trusting them', () => {
    expect(parseMe(null).ok).toBe(false);
    expect(parseMe({}).ok).toBe(false);
    expect(parseMe({ capabilities: 'brief:approve' }).ok).toBe(false);
  });
});
