1import { describe, expect, it } from 'vitest';2import { normalizeSource } from '../src/normalize';34const N = normalizeSource;56describe('normalizeSource', () => {7 it('is formatting-invariant: comments and whitespace never change the form', () => {8 const a = `9 // strategy: farm everything10 function tick(state, memory, api) {11 /* one-time init */12 if (!memory.init) {13 memory.init = 1; // remember14 api.allocate("farmer", 620);15 }16 }17 `;18 const b = `function tick(state,memory,api){if(!memory.init){memory.init=1;api.allocate("farmer",620);}}`;19 expect(N(a)).toBe(N(b));20 });2122 it('keeps a separating space between word tokens', () => {23 expect(N('var x = new Foo()')).toBe('var x=new Foo()');24 expect(N('return typeof x')).toBe('return typeof x');25 });2627 it('is literal-sensitive: changed strings or numbers change the form', () => {28 expect(N('api.say("alpha")')).not.toBe(N('api.say("beta")'));29 expect(N('x = 100')).not.toBe(N('x = 200'));30 });3132 it('preserves string contents verbatim, including comment-looking text', () => {33 const src = 'var s = "no // comment /* here */ inside";';34 expect(N(src)).toContain('"no // comment /* here */ inside"');35 });3637 it('preserves template literals verbatim including interpolation', () => {38 const src = 'var t = `a ${x + 1} // not a comment ${`nested ${y}`}`;';39 expect(N(src)).toContain('`a ${x + 1} // not a comment ${`nested ${y}`}`');40 });4142 it('treats regex literals as atoms (slashes are not comments)', () => {43 const src = 'var re = /ab\\/[/]c/gi; var d = a / b / c;';44 const out = N(src);45 expect(out).toContain('/ab\\/[/]c/gi');46 expect(out).toContain('a/b/c');47 });4849 it('handles escaped quotes inside strings', () => {50 expect(N('var s = "he said \\"hi\\" // ok";')).toContain('"he said \\"hi\\" // ok"');51 });5253 it('is idempotent', () => {54 const src = 'function f(a, b) { return a + b; } // sum';55 expect(N(N(src))).toBe(N(src));56 });5758 it('survives unterminated constructs without hanging', () => {59 expect(() => N('var s = "unterminated')).not.toThrow();60 expect(() => N('/* never closed')).not.toThrow();61 expect(() => N('var t = `open ${')).not.toThrow();62 });63});