|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { composeAuthorColorResolver, fallbackAuthorColor, stampTrackedChangeColors } from './author-colors.js'; |
| 3 | +import type { FlowBlock, ParagraphBlock, TextRun } from './index.js'; |
| 4 | + |
| 5 | +describe('composeAuthorColorResolver', () => { |
| 6 | + it('returns undefined when config is missing or disabled', () => { |
| 7 | + expect(composeAuthorColorResolver(undefined)).toBeUndefined(); |
| 8 | + expect(composeAuthorColorResolver(null)).toBeUndefined(); |
| 9 | + expect(composeAuthorColorResolver({ enabled: false, overrides: { a: '#fff' } })).toBeUndefined(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('resolves overrides by email first, then name (exact match)', () => { |
| 13 | + const resolve = composeAuthorColorResolver({ |
| 14 | + overrides: { 'a@x.test': '#111111', Alice: '#222222' }, |
| 15 | + })!; |
| 16 | + expect(resolve({ email: 'a@x.test', name: 'Alice' })).toBe('#111111'); |
| 17 | + expect(resolve({ name: 'Alice' })).toBe('#222222'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('falls through to resolve() when no override matches', () => { |
| 21 | + const resolve = composeAuthorColorResolver({ |
| 22 | + overrides: { Bob: '#000000' }, |
| 23 | + resolve: (author) => (author.name === 'Alice' ? '#abcabc' : undefined), |
| 24 | + })!; |
| 25 | + expect(resolve({ name: 'Alice' })).toBe('#abcabc'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('uses a deterministic fallback when overrides and resolve decline', () => { |
| 29 | + const resolve = composeAuthorColorResolver({ resolve: () => undefined })!; |
| 30 | + const first = resolve({ name: 'Discovered Author' }); |
| 31 | + const second = resolve({ name: 'Discovered Author' }); |
| 32 | + expect(first).toMatch(/^#[0-9a-f]{6}$/i); |
| 33 | + expect(first).toBe(second); |
| 34 | + expect(first).toBe(fallbackAuthorColor({ name: 'Discovered Author' })); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not throw when the host resolver throws', () => { |
| 38 | + const resolve = composeAuthorColorResolver({ |
| 39 | + resolve: () => { |
| 40 | + throw new Error('boom'); |
| 41 | + }, |
| 42 | + })!; |
| 43 | + expect(resolve({ name: 'Alice' })).toMatch(/^#[0-9a-f]{6}$/i); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('stampTrackedChangeColors', () => { |
| 48 | + const makeParagraph = (run: TextRun): ParagraphBlock => ({ |
| 49 | + kind: 'paragraph', |
| 50 | + id: 'p1', |
| 51 | + runs: [run], |
| 52 | + }); |
| 53 | + |
| 54 | + it('stamps color on every tracked-change layer from the author identity', () => { |
| 55 | + const run: TextRun = { |
| 56 | + kind: 'text', |
| 57 | + text: 'hi', |
| 58 | + fontFamily: 'Arial', |
| 59 | + fontSize: 12, |
| 60 | + trackedChanges: [ |
| 61 | + { kind: 'insert', id: 'tc1', author: 'Alice' }, |
| 62 | + { kind: 'format', id: 'tc2', author: 'Bob' }, |
| 63 | + ], |
| 64 | + }; |
| 65 | + run.trackedChange = run.trackedChanges![0]; |
| 66 | + |
| 67 | + const blocks: FlowBlock[] = [makeParagraph(run)]; |
| 68 | + stampTrackedChangeColors(blocks, composeAuthorColorResolver({ overrides: { Alice: '#123456', Bob: '#654321' } })!); |
| 69 | + |
| 70 | + expect(run.trackedChanges![0]!.color).toBe('#123456'); |
| 71 | + expect(run.trackedChanges![1]!.color).toBe('#654321'); |
| 72 | + // trackedChange mirror (first layer) is colored too. |
| 73 | + expect(run.trackedChange!.color).toBe('#123456'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('clears stale colors when no resolver is provided', () => { |
| 77 | + const run: TextRun = { |
| 78 | + kind: 'text', |
| 79 | + text: 'hi', |
| 80 | + fontFamily: 'Arial', |
| 81 | + fontSize: 12, |
| 82 | + trackedChanges: [{ kind: 'insert', id: 'tc1', author: 'Alice', color: '#123456' }], |
| 83 | + }; |
| 84 | + run.trackedChange = run.trackedChanges![0]; |
| 85 | + |
| 86 | + stampTrackedChangeColors([makeParagraph(run)], undefined); |
| 87 | + |
| 88 | + expect(run.trackedChanges![0]!.color).toBeUndefined(); |
| 89 | + expect(run.trackedChange!.color).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('leaves runs without tracked changes untouched', () => { |
| 93 | + const run: TextRun = { kind: 'text', text: 'plain', fontFamily: 'Arial', fontSize: 12 }; |
| 94 | + stampTrackedChangeColors([makeParagraph(run)], composeAuthorColorResolver({ overrides: {} })!); |
| 95 | + expect((run as TextRun).color).toBeUndefined(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments