contact-read.test.ts (1839B)
1 import { describe, expect, it } from 'vitest'; 2 import { readPublicContact } from '../../src/lib/contracts/food-availability-v1/contact-read.ts'; 3 describe('dedicated optional public contact', () => { 4 it('accepts only the writer final block with canonical safe contact', () => { 5 for (const href of [ 6 'mailto:seller@example.org', 7 'tel:+12025550123', 8 'https://example.org/contact' 9 ]) 10 expect( 11 readPublicContact(`Carrots\n\nPublic contact: ${href}`)?.href 12 ).toBe(href); 13 }); 14 it('never infers prose, unsafe or malformed contact', () => { 15 for (const content of [ 16 undefined, 17 {}, 18 'https://example.org', 19 'Public contact: mailto:seller@example.org', 20 'Carrots\n\nPublic contact: javascript:alert(1)', 21 'Carrots\n\nPublic contact: https://example.org\nOther text', 22 'Carrots\n\nPublic contact: mailto:a@example.org?subject=secret', 23 'Carrots\n\nPublic contact: https://user:pass@example.org', 24 'Carrots\n\nPublic contact: https://example.org/%0a' 25 ]) 26 expect(readPublicContact(content)).toBeUndefined(); 27 }); 28 it('rejects hinted relays, remote resource schemes and nested contact injection', () => { 29 for (const href of [ 30 'wss://relay.example.org', 31 'http://contact.example.org', 32 'data:text/html,hello', 33 'blob:https://contact.example.org/id', 34 '//contact.example.org', 35 'https://user@contact.example.org', 36 'https://contact.example.org/%0d%0aHeader:value', 37 'mailto:seller@example.org?body=private', 38 'mailto:seller@example.org%0aBcc:other@example.org', 39 'tel:+12025550123;ext=1', 40 'tel:12025550123', 41 'https://contact.example.org/' + 'a'.repeat(8192) 42 ]) 43 expect( 44 readPublicContact(`Carrots\n\nPublic contact: ${href}`) 45 ).toBeUndefined(); 46 }); 47 });