|
| 1 | +import { describe, it, expect, beforeAll } from "vitest"; |
| 2 | +import { readFileSync } from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +// Import the WASM module directly |
| 9 | +import init, { WitBindgen } from "../wit-bindgen-wasm/pkg/wit_bindgen_wasm.js"; |
| 10 | + |
| 11 | +const TEST_WIT = `package example:test; |
| 12 | +
|
| 13 | +world test-world { |
| 14 | + export greet: func(name: string) -> string; |
| 15 | +} |
| 16 | +`; |
| 17 | + |
| 18 | +describe("Bindings Generation for All Languages", () => { |
| 19 | + let witBindgen: WitBindgen; |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + // Initialize WASM module using the built file |
| 23 | + const wasmPath = path.join(__dirname, "../wit-bindgen-wasm/pkg/wit_bindgen_wasm_bg.wasm"); |
| 24 | + const wasmBuffer = readFileSync(wasmPath); |
| 25 | + await init(wasmBuffer); |
| 26 | + |
| 27 | + // Create WitBindgen instance |
| 28 | + witBindgen = new WitBindgen(); |
| 29 | + }); |
| 30 | + |
| 31 | + const supportedLanguages = [ |
| 32 | + { lang: "rust", extension: ".rs", expectedContent: "fn greet", minLength: 100 }, |
| 33 | + { lang: "c", extension: ".h", expectedContent: "uint8_t", minLength: 100 }, |
| 34 | + { lang: "cpp", extension: ".h", expectedContent: "namespace", minLength: 100 }, |
| 35 | + { lang: "go", extension: ".go", expectedContent: "package", minLength: 100 }, |
| 36 | + { lang: "csharp", extension: ".cs", expectedContent: "namespace", minLength: 100 }, |
| 37 | + { lang: "moonbit", extension: ".mbt", expectedContent: "Generated by", minLength: 30 }, |
| 38 | + { lang: "markdown", extension: ".md", expectedContent: "World test-world", minLength: 100 }, |
| 39 | + ]; |
| 40 | + |
| 41 | + supportedLanguages.forEach(({ lang, extension, expectedContent, minLength }) => { |
| 42 | + it(`should generate actual code stubs for ${lang}`, () => { |
| 43 | + const resultJson = witBindgen.generateBindings(TEST_WIT, lang, undefined); |
| 44 | + const result = JSON.parse(resultJson); |
| 45 | + |
| 46 | + // Verify files were generated |
| 47 | + expect(Object.keys(result).length).toBeGreaterThan(0); |
| 48 | + |
| 49 | + // Verify at least one file has the expected extension |
| 50 | + const hasExpectedFile = Object.keys(result).some((filename) => filename.endsWith(extension)); |
| 51 | + expect(hasExpectedFile).toBe(true); |
| 52 | + |
| 53 | + // Find the main file with the expected extension |
| 54 | + const mainFile = Object.entries(result).find(([filename]) => filename.endsWith(extension)); |
| 55 | + expect(mainFile).toBeDefined(); |
| 56 | + |
| 57 | + if (mainFile) { |
| 58 | + const [filename, content] = mainFile as [string, string]; |
| 59 | + |
| 60 | + // Convert from latin1 encoding |
| 61 | + const buffer = Buffer.from(content, "latin1"); |
| 62 | + const text = buffer.toString("utf8"); |
| 63 | + |
| 64 | + // Verify the file contains actual code, not just error messages |
| 65 | + expect(text.length).toBeGreaterThan(minLength); |
| 66 | + |
| 67 | + // Should not contain deprecation/unsupported messages |
| 68 | + expect(text).not.toContain("no longer supported"); |
| 69 | + expect(text).not.toContain("deprecated"); |
| 70 | + expect(text).not.toContain("has been removed"); |
| 71 | + expect(text).not.toContain("migrate to"); |
| 72 | + |
| 73 | + // Should contain expected language-specific content |
| 74 | + expect(text).toContain(expectedContent); |
| 75 | + |
| 76 | + console.log(`✅ ${lang}: Generated ${filename} (${text.length} bytes)`); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it(`should not generate only README files for ${lang}`, () => { |
| 81 | + const resultJson = witBindgen.generateBindings(TEST_WIT, lang, undefined); |
| 82 | + const result = JSON.parse(resultJson); |
| 83 | + |
| 84 | + // Verify that not all files are README files |
| 85 | + const allFilenames = Object.keys(result); |
| 86 | + const onlyReadmes = allFilenames.every( |
| 87 | + (f) => f.toLowerCase().includes("readme") || f.toLowerCase().includes("read-me") |
| 88 | + ); |
| 89 | + |
| 90 | + expect(onlyReadmes).toBe(false); |
| 91 | + |
| 92 | + // Verify at least one non-README code file exists |
| 93 | + const hasCodeFile = allFilenames.some( |
| 94 | + (f) => !f.toLowerCase().includes("readme") && (f.endsWith(extension) || f.includes(".")) |
| 95 | + ); |
| 96 | + |
| 97 | + expect(hasCodeFile).toBe(true); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should generate different output for each language", () => { |
| 102 | + const results: Record<string, Record<string, string>> = {}; |
| 103 | + |
| 104 | + for (const { lang } of supportedLanguages) { |
| 105 | + const resultJson = witBindgen.generateBindings(TEST_WIT, lang, undefined); |
| 106 | + results[lang] = JSON.parse(resultJson); |
| 107 | + } |
| 108 | + |
| 109 | + // Compare each pair to ensure they're different |
| 110 | + const languages = supportedLanguages.map((l) => l.lang); |
| 111 | + for (let i = 0; i < languages.length; i++) { |
| 112 | + for (let j = i + 1; j < languages.length; j++) { |
| 113 | + const lang1 = languages[i]; |
| 114 | + const lang2 = languages[j]; |
| 115 | + |
| 116 | + // Get file lists |
| 117 | + const files1 = Object.keys(results[lang1]); |
| 118 | + const files2 = Object.keys(results[lang2]); |
| 119 | + |
| 120 | + // Languages should generate different sets of files |
| 121 | + const sameFiles = JSON.stringify(files1.sort()) === JSON.stringify(files2.sort()); |
| 122 | + |
| 123 | + if (!sameFiles) { |
| 124 | + // Different file lists is good |
| 125 | + expect(sameFiles).toBe(false); |
| 126 | + } else { |
| 127 | + // If same file lists, content should be different |
| 128 | + const firstFile1 = files1[0]; |
| 129 | + const content1 = results[lang1][firstFile1]; |
| 130 | + const content2 = results[lang2][firstFile1]; |
| 131 | + |
| 132 | + expect(content1).not.toEqual(content2); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it("should handle complex WIT definitions for all languages", () => { |
| 139 | + const complexWit = `package example:complex; |
| 140 | +
|
| 141 | +interface logger { |
| 142 | + enum level { |
| 143 | + debug, |
| 144 | + info, |
| 145 | + warn, |
| 146 | + error, |
| 147 | + } |
| 148 | +
|
| 149 | + record entry { |
| 150 | + level: level, |
| 151 | + message: string, |
| 152 | + } |
| 153 | +
|
| 154 | + log: func(entry: entry); |
| 155 | +} |
| 156 | +
|
| 157 | +world app { |
| 158 | + import logger; |
| 159 | + export run: func() -> result<_, string>; |
| 160 | +} |
| 161 | +`; |
| 162 | + |
| 163 | + for (const { lang, extension } of supportedLanguages) { |
| 164 | + const resultJson = witBindgen.generateBindings(complexWit, lang, undefined); |
| 165 | + const result = JSON.parse(resultJson); |
| 166 | + |
| 167 | + expect(Object.keys(result).length).toBeGreaterThan(0); |
| 168 | + |
| 169 | + const hasExpectedFile = Object.keys(result).some((filename) => filename.endsWith(extension)); |
| 170 | + expect(hasExpectedFile).toBe(true); |
| 171 | + |
| 172 | + console.log(`✅ ${lang}: Successfully handled complex WIT (${Object.keys(result).length} files)`); |
| 173 | + } |
| 174 | + }); |
| 175 | +}); |
0 commit comments