|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +import { resolve } from 'path'; |
| 4 | +import { parse } from '../packages/core/src/parser/parse.js'; |
| 5 | +import { |
| 6 | + minify, |
| 7 | + minifyWithStats, |
| 8 | +} from '../packages/core/src/minifier/index.js'; |
| 9 | + |
| 10 | +const fixtureDir = resolve(__dirname, 'fixtures'); |
| 11 | + |
| 12 | +function loadFixture(name: string): unknown { |
| 13 | + return JSON.parse(readFileSync(resolve(fixtureDir, name), 'utf-8')); |
| 14 | +} |
| 15 | + |
| 16 | +function collectIds(obj: unknown, out: Set<string> = new Set()): Set<string> { |
| 17 | + if (obj && typeof obj === 'object') { |
| 18 | + if (!Array.isArray(obj)) { |
| 19 | + const o = obj as Record<string, unknown>; |
| 20 | + if (typeof o.id === 'string') out.add(o.id); |
| 21 | + for (const v of Object.values(o)) collectIds(v, out); |
| 22 | + } else { |
| 23 | + for (const v of obj) collectIds(v, out); |
| 24 | + } |
| 25 | + } |
| 26 | + return out; |
| 27 | +} |
| 28 | + |
| 29 | +describe('minify (safe mode)', () => { |
| 30 | + const input = loadFixture('HelloXircuits.xircuits'); |
| 31 | + |
| 32 | + it('produces valid .xircuits JSON that parse() accepts', () => { |
| 33 | + const out = minify(input); |
| 34 | + const reparsed = JSON.parse(out); |
| 35 | + expect(() => parse(reparsed)).not.toThrow(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('preserves every original ID byte-for-byte', () => { |
| 39 | + const out = minify(input); |
| 40 | + const reparsed = JSON.parse(out); |
| 41 | + const originalIds = collectIds(input); |
| 42 | + const outputIds = collectIds(reparsed); |
| 43 | + for (const id of originalIds) { |
| 44 | + expect(outputIds.has(id)).toBe(true); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('strips selected and locked keys throughout', () => { |
| 49 | + const out = minify(input); |
| 50 | + expect(out).not.toMatch(/"selected":/); |
| 51 | + expect(out).not.toMatch(/"locked":/); |
| 52 | + }); |
| 53 | + |
| 54 | + it('rounds coordinates to the requested precision', () => { |
| 55 | + const out = minify(input, { precision: 0 }); |
| 56 | + const reparsed = JSON.parse(out) as Record<string, unknown>; |
| 57 | + const layers = reparsed.layers as Array<Record<string, unknown>>; |
| 58 | + const nodeLayer = layers.find(l => l.type === 'diagram-nodes')!; |
| 59 | + for (const node of Object.values(nodeLayer.models as Record<string, Record<string, unknown>>)) { |
| 60 | + expect(Number.isInteger(node.x)).toBe(true); |
| 61 | + expect(Number.isInteger(node.y)).toBe(true); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('honors non-zero precision', () => { |
| 66 | + const out = minify(input, { precision: 1 }); |
| 67 | + const reparsed = JSON.parse(out) as Record<string, unknown>; |
| 68 | + const layers = reparsed.layers as Array<Record<string, unknown>>; |
| 69 | + const nodeLayer = layers.find(l => l.type === 'diagram-nodes')!; |
| 70 | + for (const node of Object.values(nodeLayer.models as Record<string, Record<string, unknown>>)) { |
| 71 | + const x = node.x as number; |
| 72 | + expect(Math.abs(x * 10 - Math.round(x * 10))).toBeLessThan(1e-9); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('reports consistent stats', () => { |
| 77 | + const { output, stats } = minifyWithStats(input); |
| 78 | + expect(stats.outputBytes).toBe(new TextEncoder().encode(output).length); |
| 79 | + expect(stats.mode).toBe('safe'); |
| 80 | + expect(stats.nodes).toBeGreaterThan(0); |
| 81 | + expect(stats.edges).toBeGreaterThan(0); |
| 82 | + expect(stats.ports).toBeGreaterThan(0); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('minify (aggressive mode)', () => { |
| 87 | + const input = loadFixture('HelloXircuits.xircuits'); |
| 88 | + |
| 89 | + it('produces valid .xircuits JSON that parse() accepts', () => { |
| 90 | + const out = minify(input, { aggressive: true }); |
| 91 | + const reparsed = JSON.parse(out); |
| 92 | + expect(() => parse(reparsed)).not.toThrow(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('replaces every original ID with a short form', () => { |
| 96 | + const out = minify(input, { aggressive: true }); |
| 97 | + const reparsed = JSON.parse(out); |
| 98 | + const originalIds = collectIds(input); |
| 99 | + const outputIds = collectIds(reparsed); |
| 100 | + for (const id of originalIds) { |
| 101 | + expect(outputIds.has(id)).toBe(false); |
| 102 | + } |
| 103 | + for (const id of outputIds) { |
| 104 | + expect(id === 'g' || /^[nep]\d+$/.test(id)).toBe(true); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('still resolves every link endpoint to a real node + port', () => { |
| 109 | + const out = minify(input, { aggressive: true }); |
| 110 | + const graph = parse(JSON.parse(out)); |
| 111 | + const nodeIds = new Set(graph.nodes.map(n => n.id)); |
| 112 | + const portIds = new Set(graph.nodes.flatMap(n => [...n.portsIn, ...n.portsOut]).map(p => p.id)); |
| 113 | + |
| 114 | + for (const edge of graph.edges) { |
| 115 | + expect(nodeIds.has(edge.sourceNodeId)).toBe(true); |
| 116 | + expect(nodeIds.has(edge.targetNodeId)).toBe(true); |
| 117 | + expect(portIds.has(edge.sourcePortId)).toBe(true); |
| 118 | + expect(portIds.has(edge.targetPortId)).toBe(true); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + it('preserves port ordering after rename', () => { |
| 123 | + const src = input as Record<string, unknown>; |
| 124 | + const origLayers = src.layers as Array<Record<string, unknown>>; |
| 125 | + const origNodeLayer = origLayers.find(l => l.type === 'diagram-nodes')!; |
| 126 | + const origModels = origNodeLayer.models as Record<string, Record<string, unknown>>; |
| 127 | + |
| 128 | + const out = minify(input, { aggressive: true }); |
| 129 | + const reparsed = JSON.parse(out) as Record<string, unknown>; |
| 130 | + const newLayers = reparsed.layers as Array<Record<string, unknown>>; |
| 131 | + const newNodeLayer = newLayers.find(l => l.type === 'diagram-nodes')!; |
| 132 | + const newModels = newNodeLayer.models as Record<string, Record<string, unknown>>; |
| 133 | + |
| 134 | + const origNodes = Object.values(origModels); |
| 135 | + const newNodes = Object.values(newModels); |
| 136 | + expect(newNodes.length).toBe(origNodes.length); |
| 137 | + |
| 138 | + for (let i = 0; i < origNodes.length; i++) { |
| 139 | + const origInCount = (origNodes[i].portsInOrder as string[]).length; |
| 140 | + const origOutCount = (origNodes[i].portsOutOrder as string[]).length; |
| 141 | + expect((newNodes[i].portsInOrder as string[]).length).toBe(origInCount); |
| 142 | + expect((newNodes[i].portsOutOrder as string[]).length).toBe(origOutCount); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it('strips viewer-unused fields', () => { |
| 147 | + const out = minify(input, { aggressive: true }); |
| 148 | + expect(out).not.toMatch(/"curvyness":/); |
| 149 | + expect(out).not.toMatch(/"selectedColor":/); |
| 150 | + expect(out).not.toMatch(/"isSvg":/); |
| 151 | + expect(out).not.toMatch(/"transformed":/); |
| 152 | + expect(out).not.toMatch(/"alignment":/); |
| 153 | + expect(out).not.toMatch(/"parentNode":/); |
| 154 | + }); |
| 155 | + |
| 156 | + it('beats safe mode on size', () => { |
| 157 | + const safe = minify(input, { aggressive: false }); |
| 158 | + const aggr = minify(input, { aggressive: true }); |
| 159 | + expect(aggr.length).toBeLessThan(safe.length); |
| 160 | + }); |
| 161 | + |
| 162 | + it('tags stats as aggressive', () => { |
| 163 | + const { stats } = minifyWithStats(input, { aggressive: true }); |
| 164 | + expect(stats.mode).toBe('aggressive'); |
| 165 | + }); |
| 166 | +}); |
0 commit comments