|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { shouldDisplay, toBadge } from './badge.js'; |
| 4 | +import { readProvenance } from './read.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The badge is the only part of this package a user ever sees, so the wording |
| 8 | + * is the part that can mislead. These tests are mostly about what it refuses |
| 9 | + * to say: there is no "authentic" state, absence never reads as real, and a |
| 10 | + * tool name never becomes an accusation. |
| 11 | + */ |
| 12 | + |
| 13 | +/** Builds bytes from literal bytes and ASCII strings. */ |
| 14 | +function bytes(...parts: (number[] | string)[]): Uint8Array { |
| 15 | + const out: number[] = []; |
| 16 | + for (const part of parts) { |
| 17 | + if (typeof part === 'string') { |
| 18 | + for (let i = 0; i < part.length; i += 1) out.push(part.charCodeAt(i)); |
| 19 | + } else { |
| 20 | + out.push(...part); |
| 21 | + } |
| 22 | + } |
| 23 | + return new Uint8Array(out); |
| 24 | +} |
| 25 | + |
| 26 | +const JPEG = [0xff, 0xd8, 0xff, 0xe0]; |
| 27 | +const PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; |
| 28 | + |
| 29 | +function xmp(inner: string): string { |
| 30 | + return `<x:xmpmeta xmlns:x="adobe:ns:meta/">${inner}</x:xmpmeta>`; |
| 31 | +} |
| 32 | + |
| 33 | +function sourceType(term: string): string { |
| 34 | + return xmp( |
| 35 | + `<rdf:Description Iptc4xmpExt:DigitalSourceType="http://cv.iptc.org/newscodes/digitalsourcetype/${term}"/>`, |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +describe('what the badge says', () => { |
| 40 | + it('calls a declared generative file AI-generated, and shows it unprompted', () => { |
| 41 | + const badge = toBadge(readProvenance(bytes(JPEG, sourceType('trainedAlgorithmicMedia')))); |
| 42 | + |
| 43 | + expect(badge.kind).toBe('ai'); |
| 44 | + expect(badge.label).toBe('AI-generated'); |
| 45 | + expect(badge.prominent).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('reports a camera claim as a claim, not as authenticity', () => { |
| 49 | + const badge = toBadge(readProvenance(bytes(JPEG, sourceType('digitalCapture')))); |
| 50 | + |
| 51 | + expect(badge.kind).toBe('camera'); |
| 52 | + expect(badge.label).toBe('Camera capture'); |
| 53 | + // Nothing here can establish a photo is genuine, so nothing says so. |
| 54 | + expect(badge.label).not.toMatch(/authentic|verified|genuine|real/i); |
| 55 | + expect(badge.detail).toMatch(/not a signature/i); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not interrupt the page for an unsigned camera claim', () => { |
| 59 | + const unsigned = toBadge(readProvenance(bytes(JPEG, sourceType('digitalCapture')))); |
| 60 | + const signed = toBadge(readProvenance(bytes(PNG, 'caBX', sourceType('digitalCapture')))); |
| 61 | + |
| 62 | + expect(unsigned.prominent).toBe(false); |
| 63 | + expect(signed.prominent).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('reports a manifest that declares nothing as exactly that', () => { |
| 67 | + const badge = toBadge(readProvenance(bytes(PNG, 'caBX', 'manifest'))); |
| 68 | + |
| 69 | + expect(badge.kind).toBe('signed'); |
| 70 | + expect(badge.detail).toMatch(/does not declare how it was made/i); |
| 71 | + expect(badge.prominent).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('treats a generator name as a hint and keeps it off the page', () => { |
| 75 | + const badge = toBadge(readProvenance(bytes(JPEG, xmp('<xmp:CreatorTool>Midjourney</xmp:CreatorTool>')))); |
| 76 | + |
| 77 | + expect(badge.kind).toBe('hint'); |
| 78 | + expect(badge.label).toBe('Possible AI tool'); |
| 79 | + // "Opened in Firefly" must not train people to read the badge as "fake". |
| 80 | + expect(badge.prominent).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it('never turns a bare file into a claim about it', () => { |
| 84 | + const badge = toBadge(readProvenance(bytes(JPEG, 'ordinary pixels'))); |
| 85 | + |
| 86 | + expect(badge.kind).toBe('unknown'); |
| 87 | + expect(badge.prominent).toBe(false); |
| 88 | + expect(badge.detail).toMatch(/not evidence either way/i); |
| 89 | + }); |
| 90 | + |
| 91 | + it('attaches the report caveats to every badge', () => { |
| 92 | + const samples = [ |
| 93 | + bytes(JPEG, sourceType('trainedAlgorithmicMedia')), |
| 94 | + bytes(JPEG, sourceType('digitalCapture')), |
| 95 | + bytes(PNG, 'caBX'), |
| 96 | + bytes(JPEG, xmp('<xmp:CreatorTool>Midjourney</xmp:CreatorTool>')), |
| 97 | + bytes(JPEG, 'nothing'), |
| 98 | + ]; |
| 99 | + |
| 100 | + for (const sample of samples) { |
| 101 | + const report = readProvenance(sample); |
| 102 | + const badge = toBadge(report); |
| 103 | + // The caveat is what stops the badge overclaiming, so it is never optional. |
| 104 | + for (const note of report.notes) expect(badge.detail).toContain(note); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('never labels anything as verified', () => { |
| 109 | + const samples = [ |
| 110 | + bytes(PNG, 'caBX', sourceType('digitalCapture')), |
| 111 | + bytes(PNG, 'caBX'), |
| 112 | + bytes(JPEG, sourceType('digitalCapture')), |
| 113 | + ]; |
| 114 | + |
| 115 | + for (const sample of samples) { |
| 116 | + expect(toBadge(readProvenance(sample)).label).not.toMatch(/verified|authentic/i); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('shouldDisplay', () => { |
| 122 | + it('draws findings that say something and holds back the rest', () => { |
| 123 | + const ai = toBadge(readProvenance(bytes(JPEG, sourceType('trainedAlgorithmicMedia')))); |
| 124 | + const nothing = toBadge(readProvenance(bytes(JPEG, 'plain'))); |
| 125 | + const hint = toBadge(readProvenance(bytes(JPEG, xmp('<x>Midjourney</x>')))); |
| 126 | + |
| 127 | + expect(shouldDisplay(ai)).toBe(true); |
| 128 | + expect(shouldDisplay(nothing)).toBe(false); |
| 129 | + expect(shouldDisplay(hint)).toBe(false); |
| 130 | + }); |
| 131 | +}); |
0 commit comments