import { describe, expect, it } from 'vitest' import { CHART_COLORS, barChart, columnChart, lineChart, proportionalPercent, stackedColumnChart, valueList, } from '../../scripts/lib/chartPrimitives.mjs' // C191: the chart primitives were extracted from dossieRender.mjs so the // Instagram generator (second consumer) never grows a twin. These snapshots // freeze the dossier emission byte-for-byte — a change here is a change to the // published dossiê, not a cosmetic tweak. describe('chartPrimitives — dossier emitters (byte-identical)', () => { it('barChart', () => { expect( barChart({ rows: [ { label: 'A', count: 3 }, { label: 'B', count: 1 }, ], }), ).toMatchInlineSnapshot(` " A 3B 1 " `) }) it('columnChart', () => { expect( columnChart({ rows: [ { key: '2020', value: 2 }, { key: '2021', value: 4 }, ], format: (row: { value: number }) => String(row.value), }), ).toMatchInlineSnapshot(` " 2 20 4 21 " `) }) it('stackedColumnChart', () => { expect( stackedColumnChart({ rows: [ { key: '2020', value: 3, segments: [ { key: 'pago', amount: 2 }, { key: 'empenhado', amount: 1 }, ], }, ], format: (row: { value: number }) => String(row.value), }), ).toMatchInlineSnapshot(` " 3 20 " `) }) it('valueList', () => { expect( valueList({ rows: [ { label: 'A', value: 2 }, { label: 'B', value: 1 }, ], format: (row: { value: number }) => String(row.value), }), ).toMatchInlineSnapshot(` "" `) }) it('escapes untrusted labels', () => { expect(barChart({ rows: [{ label: 'x', count: 1 }] })).toContain('<b>') }) it('keeps the dossier domain palette as the default', () => { expect(CHART_COLORS.default).toBe('#315c75') expect(CHART_COLORS.pago).toBe('#285338') }) }) describe('proportionalPercent — honest scale, zero stays zero', () => { it('maps value/max to 0..100 and floors a present value at 2%', () => { expect(proportionalPercent(84, 84)).toBe(100) expect(proportionalPercent(42, 84)).toBe(50) expect(proportionalPercent(1, 84)).toBe(2) expect(proportionalPercent(0, 84)).toBe(0) expect(proportionalPercent(5, 0)).toBe(100) }) }) describe('lineChart — series with one highlighted end', () => { const rows = [ { label: '2020', value: 2 }, { label: '2021', value: 4 }, ] const svg = lineChart({ rows, format: (row: { value: number }) => String(row.value) }) it('draws every label and the last value', () => { expect(svg).toContain('>2020<') expect(svg).toContain('>2021<') expect(svg).toContain('>4<') }) it('marks the last point with the Solla red', () => { expect(svg).toContain('fill="#c51414"') }) })