jevstrudel.git / packages / transpiler / test / transpiler.test.mjs

transpiler.test.mjs - <short description TODO> Copyright (C) 2022 Strudel contributors - see https://codeberg.org/uzu/strudel/src/branch/main/packages/transpiler/test/transpiler.test.mjs This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

7import { describe, it, expect } from 'vitest';
8import { transpiler } from '../index.mjs';
10const simple = { wrapAsync: false, addReturn: false, simpleLocs: true };
11
12describe('transpiler', () => {
13  it('wraps double quote string with mini and adds location', () => {
14    expect(transpiler('"c3"', simple).output).toEqual("m('c3', 0);");
15    expect(transpiler('stack("c3","bd sd")', simple).output).toEqual("stack(m('c3', 6), m('bd sd', 11));");
16  });
17  it('wraps backtick string with mini and adds location', () => {
18    expect(transpiler('`c3`', simple).output).toEqual("m('c3', 0);");
19  });
20  it('keeps tagged template literal as is', () => {
21    expect(transpiler('xxx`c3`', simple).output).toEqual('xxx`c3`;');
22  });
23  it('supports top level await', () => {
24    expect(transpiler("await samples('xxx');", simple).output).toEqual("await samples('xxx');");
25  });
26  it('adds await to bare samples call', () => {
27    expect(transpiler("samples('xxx');", simple).output).toEqual("await samples('xxx');");
28  });
29  it('handles mini strings in K(...)', () => {
30    expect(transpiler('K("bd sd")', simple).output).toEqual("worklet('pat[0]', m('bd sd', 2));");
31  });
32  it('treats K(...) as kabelsalat', () => {
33    expect(transpiler('K(1+2)', simple).output).toEqual("worklet('1 + 2');");
34  });
35  it('automatically calls functions in K(...)', () => {
36    expect(transpiler('K(() => { return 1 + 2 })', simple).output).toEqual(
37      "worklet('(() => {\\n    return 1 + 2\\n})()');",
38    );
39  });
40  it('handles strudel S(...) inside kabelsalat K(...)', () => {
41    expect(transpiler('K(S("bd".fast(4)))', simple).output).toEqual("worklet('pat[0]', m('bd', 4).fast(4));");
42  });
43  /*   it('parses dynamic imports', () => {
44    expect(
45      transpiler("const { default: foo } = await import('https://bar.com/foo.js');", {
46        wrapAsync: false,
47        addReturn: false,
48      }),
49    ).toEqual("const {default: foo} = await import('https://bar.com/foo.js');");
50  }); */
51  it('collections locations', () => {
52    const { miniLocations } = transpiler(`s("bd", "hh oh")`, { ...simple, emitMiniLocations: true });
53    expect(miniLocations).toEqual([
54      [3, 5],
55      [9, 11],
56      [12, 14],
57    ]);
58  });
59  it('allows disabling mini', () => {
60    const code = `/* mini-off */
61      const randPrefix = Math.random() > 0.5 ? "b" : "s";
62      const drumPat = \`\${randPrefix}d\`;
63      // mini-on
64      s(drumPat).lpf("5000 10000") // make sure mini still runs;
65    `;
66    const { output, miniLocations } = transpiler(code, { ...simple, emitMiniLocations: true });
67    expect(output).not.toContain("m('b'");
68    expect(output).not.toContain("m('s'");
69    const cutoffIdx = code.indexOf('5000 10000');
70    expect(miniLocations).toHaveLength(2);
71    expect(miniLocations[0][0]).toEqual(cutoffIdx);
72  });
73});