app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

support-config.test.ts (4819B)


      1 import { describe, expect, it } from 'vitest';
      2 import {
      3   validateSupportPolicy,
      4   readSupportPolicy,
      5   requireLaunchSupportPolicy,
      6   type SupportPolicy
      7 } from '../../src/lib/config/support.ts';
      8 
      9 // Parser-only, explicitly controlled values. No destination is contacted or
     10 // presented as the actual deployment's approved support owner/destination.
     11 const fixture = {
     12   schemaVersion: 1,
     13   owner: 'Controlled fixture operator',
     14   destination: 'mailto:reports@operator.org'
     15 };
     16 const raw = (fields: Record<string, unknown> = {}) =>
     17   JSON.stringify({ ...fixture, ...fields });
     18 
     19 describe('HCP124 original bounded deployment support configuration', () => {
     20   it.each([
     21     '3fff::1',
     22     '3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff',
     23     '2001:db8::1',
     24     '2001:1f::1',
     25     '2001:3f::1',
     26     '2002::1',
     27     '2620:4f:8000::1',
     28     '::ffff:808:808',
     29     'fc00::1'
     30   ])('CR006 rejects special IPv6 support destination %s', (address) => {
     31     expect(
     32       validateSupportPolicy(raw({ destination: `https://[${address}]/report` }))
     33     ).toBeUndefined();
     34   });
     35   it.each(['2606:4700:4700::1111', '3fff:1000::1', '2001:200::1'])(
     36     'CR006 preserves canonical public IPv6 HTTPS support destination %s',
     37     (address) => {
     38       const href = `https://[${address}]:8443/report`;
     39       const policy = validateSupportPolicy(raw({ destination: href }));
     40       expect(policy).toBeDefined();
     41       expect(policy && readSupportPolicy(policy).destination).toBe(href);
     42     }
     43   );
     44   it('admits the exact inert fixture and returns a detached snapshot', () => {
     45     const policy = validateSupportPolicy(raw());
     46     expect(policy).toBeDefined();
     47     if (!policy) throw Error('fixture admission');
     48     expect(readSupportPolicy(policy)).toEqual(fixture);
     49     const detached = readSupportPolicy(policy) as { owner: string };
     50     detached.owner = 'changed copy';
     51     expect(readSupportPolicy(policy).owner).toBe(fixture.owner);
     52     expect(requireLaunchSupportPolicy(policy)).toBe(policy);
     53   });
     54   it('empty development inputs are explicit and refuse launch preparation', () => {
     55     const policy = validateSupportPolicy(raw({ owner: '', destination: '' }));
     56     expect(policy).toBeDefined();
     57     if (!policy) throw Error('development policy');
     58     expect(() => requireLaunchSupportPolicy(policy)).toThrow(
     59       'support_launch_inputs_missing'
     60     );
     61   });
     62   it('accepts a reviewed HTTPS destination without acquiring transport', () => {
     63     const policy = validateSupportPolicy(
     64       raw({ destination: 'https://support.operator.org/report' })
     65     );
     66     expect(policy).toBeDefined();
     67     if (!policy) throw Error('fixture HTTPS admission');
     68     expect(readSupportPolicy(policy).destination).toBe(
     69       'https://support.operator.org/report'
     70     );
     71   });
     72   it.each([
     73     ['malformed JSON', '{'],
     74     ['unknown field', raw({ transcript: 'not allowed' })],
     75     ['wrong version', raw({ schemaVersion: 2 })],
     76     ['wrong owner type', raw({ owner: 42 })],
     77     ['wrong destination type', raw({ destination: [] })],
     78     ['owner control', raw({ owner: 'fixture\nforged owner' })],
     79     ['oversized owner', raw({ owner: 'x'.repeat(257) })],
     80     ['plaintext HTTP', raw({ destination: 'http://support.operator.org/' })],
     81     ['script scheme', raw({ destination: 'javascript:alert(1)' })],
     82     ['data carrier', raw({ destination: 'data:text/plain,report' })],
     83     ['relative route', raw({ destination: '/about#report' })],
     84     [
     85       'credentials',
     86       raw({ destination: 'https://secret@support.operator.org/' })
     87     ],
     88     [
     89       'mailto headers',
     90       raw({ destination: 'mailto:reports@operator.org?body=PRIVATE' })
     91     ],
     92     [
     93       'encoded header control',
     94       raw({ destination: 'mailto:reports%0d%0a@operator.org' })
     95     ],
     96     [
     97       'multiple mail recipients',
     98       raw({ destination: 'mailto:first@operator.org,second@operator.org' })
     99     ],
    100     [
    101       'HTTPS context query',
    102       raw({ destination: 'https://support.operator.org/report?body=PRIVATE' })
    103     ],
    104     [
    105       'HTTPS context fragment',
    106       raw({ destination: 'https://support.operator.org/report#PRIVATE' })
    107     ],
    108     ['oversized configuration', ' '.repeat(16385)],
    109     ['ill formed text', raw({ owner: '\ud800' })]
    110   ])(
    111     'rejects %s without making malformed configuration authoritative',
    112     (_label, value) => {
    113       expect(validateSupportPolicy(value)).toBeUndefined();
    114     }
    115   );
    116   it('does not evaluate caller getters or admit copied policy objects', () => {
    117     let reads = 0;
    118     const caller = {
    119       get owner() {
    120         reads++;
    121         return fixture.owner;
    122       },
    123       destination: fixture.destination,
    124       schemaVersion: 1
    125     };
    126     expect(validateSupportPolicy(caller)).toBeUndefined();
    127     expect(reads).toBe(0);
    128     expect(() => readSupportPolicy(caller as unknown as SupportPolicy)).toThrow(
    129       'support_policy_invalid'
    130     );
    131   });
    132 });