grammar-wasm-bytes.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * readGrammarWasmBytes + bytes-based grammar loading (#1231, Phase 2.1).
  3. *
  4. * The orchestrator pre-reads each needed grammar's WASM once on the main
  5. * thread and hands the bytes to every parse worker, so a worker respawn loads
  6. * grammars from memory instead of re-reading them from a (possibly slow) disk.
  7. * These tests pin that the byte reader resolves the same artifacts the loader
  8. * would, and that web-tree-sitter genuinely accepts the bytes.
  9. */
  10. import { describe, it, expect } from 'vitest';
  11. import { Parser, Language as WasmLanguage } from 'web-tree-sitter';
  12. import { readGrammarWasmBytes } from '../src/extraction/grammars';
  13. describe('readGrammarWasmBytes', () => {
  14. it('reads bytes for a tree-sitter-wasms grammar and a vendored grammar', async () => {
  15. const bytes = await readGrammarWasmBytes(['typescript', 'lua']);
  16. expect(bytes.typescript).toBeInstanceOf(Uint8Array); // from tree-sitter-wasms
  17. expect(bytes.typescript.byteLength).toBeGreaterThan(10_000);
  18. expect(bytes.lua).toBeInstanceOf(Uint8Array); // vendored under src/extraction/wasm/
  19. expect(bytes.lua.byteLength).toBeGreaterThan(10_000);
  20. });
  21. it('expands delegating languages to the grammars they need (svelte → ts/js)', async () => {
  22. const bytes = await readGrammarWasmBytes(['svelte']);
  23. expect(Object.keys(bytes).sort()).toEqual(['javascript', 'typescript']);
  24. });
  25. it('omits languages without a WASM grammar instead of failing', async () => {
  26. const bytes = await readGrammarWasmBytes(['yaml', 'unknown']);
  27. expect(Object.keys(bytes)).toEqual([]);
  28. });
  29. it('produces bytes web-tree-sitter can load into a working parser', async () => {
  30. await Parser.init();
  31. const bytes = await readGrammarWasmBytes(['javascript']);
  32. const language = await WasmLanguage.load(bytes.javascript);
  33. const parser = new Parser();
  34. parser.setLanguage(language);
  35. const tree = parser.parse('function hello() { return 1; }');
  36. expect(tree!.rootNode.hasError).toBe(false);
  37. expect(tree!.rootNode.toString()).toContain('function_declaration');
  38. });
  39. });