diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3d6e3f07..0fd74736 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,7 +8,7 @@ on: workflow_dispatch: env: - RUST_VERSION: 1.84 + RUST_VERSION: 1.94 jobs: lint_and_test: diff --git a/packages/build-tools/Cargo.lock b/packages/build-tools/Cargo.lock index 4cf0dbe8..3e8eb32b 100644 --- a/packages/build-tools/Cargo.lock +++ b/packages/build-tools/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "anyhow" version = "1.0.95" @@ -26,12 +14,6 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "equivalent" version = "1.0.1" @@ -39,19 +21,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] -name = "hashbrown" -version = "0.14.5" +name = "foldhash" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" -dependencies = [ - "ahash", -] +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" [[package]] name = "hashbrown" -version = "0.15.2" +version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash", +] [[package]] name = "heck" @@ -61,19 +43,20 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "id-arena" -version = "2.2.1" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" [[package]] name = "indexmap" -version = "2.7.1" +version = "2.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +checksum = "45a8a2b9cb3e0b0c1803dbb0758ffac5de2f425b23c28f518faabd9d805342ff" dependencies = [ "equivalent", - "hashbrown 0.15.2", + "hashbrown", "serde", + "serde_core", ] [[package]] @@ -83,10 +66,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] -name = "leb128" -version = "0.2.5" +name = "leb128fmt" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" [[package]] name = "log" @@ -95,16 +78,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" [[package]] -name = "memchr" -version = "2.7.4" +name = "macro-string" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +checksum = "59a9dbbfc75d2688ed057456ce8a3ee3f48d12eec09229f560f3643b9f275653" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] -name = "once_cell" -version = "1.20.3" +name = "memchr" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "prettyplease" @@ -148,18 +136,27 @@ checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03" [[package]] name = "serde" -version = "1.0.217" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", @@ -178,21 +175,6 @@ dependencies = [ "serde", ] -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - -[[package]] -name = "spdx" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58b69356da67e2fc1f542c71ea7e654a361a79c938e4424392ecf4fa065d2193" -dependencies = [ - "smallvec", -] - [[package]] name = "syn" version = "2.0.98" @@ -216,143 +198,85 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - [[package]] name = "wasm-encoder" -version = "0.212.0" +version = "0.246.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501940df4418b8929eb6d52f1aade1fdd15a5b86c92453cb696e3c906bd3fc33" +checksum = "61fb705ce81adde29d2a8e99d87995e39a6e927358c91398f374474746070ef7" dependencies = [ - "leb128", - "wasmparser 0.212.0", -] - -[[package]] -name = "wasm-encoder" -version = "0.219.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8aa79bcd666a043b58f5fa62b221b0b914dd901e6f620e8ab7371057a797f3e1" -dependencies = [ - "leb128", - "wasmparser 0.219.2", -] - -[[package]] -name = "wasm-metadata" -version = "0.212.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a1849fac257fd76c43268555e73d74848c8dff23975c238c2cbad61cffe5045" -dependencies = [ - "anyhow", - "indexmap", - "serde", - "serde_derive", - "serde_json", - "spdx", - "wasm-encoder 0.212.0", - "wasmparser 0.212.0", + "leb128fmt", + "wasmparser", ] [[package]] name = "wasm-metadata" -version = "0.219.2" +version = "0.246.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1ef51bd442042a2a7b562dddb6016ead52c4abab254c376dcffc83add2c9c34" +checksum = "e3e4c2aa916c425dcca61a6887d3e135acdee2c6d0ed51fd61c08d41ddaf62b1" dependencies = [ "anyhow", "indexmap", - "serde", - "serde_derive", - "serde_json", - "spdx", - "wasm-encoder 0.219.2", - "wasmparser 0.219.2", -] - -[[package]] -name = "wasmparser" -version = "0.212.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d28bc49ba1e5c5b61ffa7a2eace10820443c4b7d1c0b144109261d14570fdf8" -dependencies = [ - "ahash", - "bitflags", - "hashbrown 0.14.5", - "indexmap", - "semver", + "wasm-encoder", + "wasmparser", ] [[package]] name = "wasmparser" -version = "0.219.2" +version = "0.246.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5220ee4c6ffcc0cb9d7c47398052203bc902c8ef3985b0c8134118440c0b2921" +checksum = "71cde4757396defafd25417cfb36aa3161027d06d865b0c24baaae229aac005d" dependencies = [ - "ahash", "bitflags", - "hashbrown 0.14.5", + "hashbrown", "indexmap", "semver", ] [[package]] name = "wit-bindgen" -version = "0.27.0" +version = "0.55.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabce76bbb8938536c437da0c3e1d4dda9065453f72a68f797c0cb3d67356a28" +checksum = "6870386de1813a61406d88749d5897484e2f6fe90a39408a6a94e160d8c72378" dependencies = [ - "wit-bindgen-rt", + "bitflags", "wit-bindgen-rust-macro", ] [[package]] name = "wit-bindgen-core" -version = "0.27.0" +version = "0.55.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b43fbdd3497c471bbfb6973b1fb9ffe6949f158248cb43171d6f1cf3de7eaa3" +checksum = "4779c97d3b9dda56600c3404355d404f8c6567fae0c4d8dfeb92f6e9b2c4c8c3" dependencies = [ "anyhow", "heck", - "wit-parser 0.212.0", -] - -[[package]] -name = "wit-bindgen-rt" -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75956ff0a04a87ca0526b07199ce3b9baee899f2e4723b5b63aa296ab172ec52" -dependencies = [ - "bitflags", + "wit-parser", ] [[package]] name = "wit-bindgen-rust" -version = "0.27.0" +version = "0.55.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf509c4ef97b18ec0218741c8318706ac30ff16bc1731f990319a42bbbcfe8e3" +checksum = "8a89a98e0efe034f47f5cf86fa8aeb5d6d7175bade32bbba476aeba29541fed9" dependencies = [ "anyhow", "heck", "indexmap", "prettyplease", "syn", - "wasm-metadata 0.212.0", + "wasm-metadata", "wit-bindgen-core", - "wit-component 0.212.0", + "wit-component", ] [[package]] name = "wit-bindgen-rust-macro" -version = "0.27.0" +version = "0.55.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d6f2e025e38395d71fc1bf064e581b2ad275ce322d6f8d87ddc5e76a7b8c42" +checksum = "8b81978b3d68d12116ae8e5ef3d2125c4cb619ea30002ed20cb7549383f6fca9" dependencies = [ "anyhow", + "macro-string", "prettyplease", "proc-macro2", "quote", @@ -363,9 +287,9 @@ dependencies = [ [[package]] name = "wit-component" -version = "0.212.0" +version = "0.246.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ed5b0f9fc3d6424787d2a49e1142bf954ae4f26ee891992c144f0cfd68c4b7f" +checksum = "1936c26cb24b93dc36bf78fb5dc35c55cd37f66ecdc2d2663a717d9fb3ee951e" dependencies = [ "anyhow", "bitflags", @@ -374,38 +298,20 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "wasm-encoder 0.212.0", - "wasm-metadata 0.212.0", - "wasmparser 0.212.0", - "wit-parser 0.212.0", -] - -[[package]] -name = "wit-component" -version = "0.219.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8479a29d81c063264c3ab89d496787ef78f8345317a2dcf6dece0f129e5fcd" -dependencies = [ - "anyhow", - "bitflags", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder 0.219.2", - "wasm-metadata 0.219.2", - "wasmparser 0.219.2", - "wit-parser 0.219.2", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", ] [[package]] name = "wit-parser" -version = "0.212.0" +version = "0.246.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ceeb0424aa8679f3fcf2d6e3cfa381f3d6fa6179976a2c05a6249dd2bb426716" +checksum = "fd979042b5ff288607ccf3b314145435453f20fc67173195f91062d2289b204d" dependencies = [ "anyhow", + "hashbrown", "id-arena", "indexmap", "log", @@ -414,25 +320,7 @@ dependencies = [ "serde_derive", "serde_json", "unicode-xid", - "wasmparser 0.212.0", -] - -[[package]] -name = "wit-parser" -version = "0.219.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca004bb251010fe956f4a5b9d4bf86b4e415064160dd6669569939e8cbf2504f" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser 0.219.2", + "wasmparser", ] [[package]] @@ -443,26 +331,6 @@ dependencies = [ "id-arena", "wit-bindgen", "wit-bindgen-core", - "wit-component 0.219.2", - "wit-parser 0.219.2", -] - -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "wit-component", + "wit-parser", ] diff --git a/packages/build-tools/Cargo.toml b/packages/build-tools/Cargo.toml index 6b0151a8..8a6f6157 100644 --- a/packages/build-tools/Cargo.toml +++ b/packages/build-tools/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" [workspace.dependencies] anyhow = "1.0.86" -wit-bindgen = "0.27.0" -wit-bindgen-core = "0.27.0" -wit-component = "0.219.1" -wit-parser = "0.219.1" +wit-bindgen = "0.55.0" +wit-bindgen-core = "0.55.0" +wit-component = "0.246.2" +wit-parser = "0.246.2" diff --git a/packages/build-tools/crates/wit-tools/src/lib.rs b/packages/build-tools/crates/wit-tools/src/lib.rs index d1586380..51dbca41 100644 --- a/packages/build-tools/crates/wit-tools/src/lib.rs +++ b/packages/build-tools/crates/wit-tools/src/lib.rs @@ -1,7 +1,7 @@ use anyhow::anyhow; use std::path::{Path, PathBuf}; use wit_component::WitPrinter; -use wit_parser::{PackageId, Resolve}; +use wit_parser::{CloneMaps, PackageId, Resolve}; wit_bindgen::generate!({ world: "tools", @@ -61,10 +61,11 @@ impl WitMerger { .base_world_id .ok_or_else(|| anyhow!("Base world not initialized"))?; + let mut clone_maps = CloneMaps::default(); for target in worlds { let (_pkg_id, world_id) = self.find_target_world(&target)?; self.resolve - .merge_worlds(world_id, base_world) + .merge_worlds(world_id, base_world, &mut clone_maps) .map_err(|e| { anyhow!( "Failed to merge world '{}/{}': {}", @@ -85,7 +86,7 @@ impl WitMerger { let mut imports = Vec::new(); for (_, item) in self.resolve.worlds[base_world].imports.iter() { match item { - wit_parser::WorldItem::Interface { id, stability: _ } => { + wit_parser::WorldItem::Interface { id, .. } => { if let Some(import_string) = self.format_interface_import(id) { imports.push(import_string); } @@ -98,7 +99,7 @@ impl WitMerger { } // Do nothing for types because ComponentizeJS does not // generate bindings for types - wit_parser::WorldItem::Type(_) => {} + wit_parser::WorldItem::Type { .. } => {} } } Ok(imports) @@ -247,7 +248,8 @@ fn generate_wit_output(resolve: &Resolve, package_id: PackageId) -> anyhow::Resu .filter(|id| *id != package_id) .collect(); - printer.print(resolve, package_id, &excluded_ids) + printer.print(resolve, package_id, &excluded_ids)?; + Ok(printer.output.into()) } export!(BuildTools); diff --git a/packages/build-tools/src/index.ts b/packages/build-tools/src/index.ts index 36930694..bd93153d 100644 --- a/packages/build-tools/src/index.ts +++ b/packages/build-tools/src/index.ts @@ -18,7 +18,6 @@ import { mergeWit } from '../lib/wit_tools.js'; import { precompile } from "./precompile.js" import path from 'node:path' import { SourceMapInput } from '@ampproject/remapping'; -import { get } from 'node:http'; async function main() { try { diff --git a/packages/build-tools/src/wasiDepsParser.ts b/packages/build-tools/src/wasiDepsParser.ts index 76eb37dd..496e8d12 100644 --- a/packages/build-tools/src/wasiDepsParser.ts +++ b/packages/build-tools/src/wasiDepsParser.ts @@ -108,18 +108,14 @@ export function getPackagesWithWasiDeps( if (topLevel) { if (packageJson.config?.witDependencies) { - if (packageJson.config.witDependencies) { - if (packageJson.config.witDependencies) { - absolutizeWitPath(dir, packageJson); - } + absolutizeWitPath(dir, packageJson); - result.push({ - name: packageJson.name, - config: { - witDependencies: packageJson.config.witDependencies, - }, - }); - } + result.push({ + name: packageJson.name, + config: { + witDependencies: packageJson.config.witDependencies, + }, + }); } } diff --git a/packages/build-tools/test/build.spec.ts b/packages/build-tools/test/build.spec.ts new file mode 100644 index 00000000..a722cb60 --- /dev/null +++ b/packages/build-tools/test/build.spec.ts @@ -0,0 +1,209 @@ +import { getBuildDataPath, ShouldComponentize } from '../dist/build.js'; +import { expect } from 'chai'; +import fs from 'fs'; +import { writeFile, mkdir } from 'node:fs/promises'; +import path from 'path'; +import os from 'os'; + +describe('getBuildDataPath', () => { + it('should append .buildData.json to the source path', () => { + expect(getBuildDataPath('src/index.js')).to.equal( + 'src/index.js.buildData.json', + ); + }); + + it('should work with absolute paths', () => { + expect(getBuildDataPath('/home/user/project/src/main.js')).to.equal( + '/home/user/project/src/main.js.buildData.json', + ); + }); + + it('should handle paths with dots', () => { + expect(getBuildDataPath('app.bundle.min.js')).to.equal( + 'app.bundle.min.js.buildData.json', + ); + }); +}); + +describe('ShouldComponentize', () => { + let tmpDir: string; + + before(async () => { + tmpDir = path.join(os.tmpdir(), `build-tools-test-sc-${Date.now()}`); + await mkdir(tmpDir, { recursive: true }); + }); + + after(async () => { + await fs.promises.rm(tmpDir, { recursive: true, force: true }); + }); + + it('should return true when no build data exists', async () => { + const srcPath = path.join(tmpDir, 'new-source.js'); + await writeFile(srcPath, 'console.log("hello");'); + const outputPath = path.join(tmpDir, 'component.wasm'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '', + 'wit-hash', + ); + expect(result).to.be.true; + }); + + it('should return true when output file does not exist', async () => { + const srcPath = path.join(tmpDir, 'existing-source.js'); + await writeFile(srcPath, 'console.log("hello");'); + + // Create build data that matches + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const checksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData(buildDataPath, checksum, '1.0.0', '', 'wit-hash'); + + // Output file doesn't exist + const outputPath = path.join(tmpDir, 'missing-output.wasm'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '', + 'wit-hash', + ); + expect(result).to.be.true; + }); + + it('should return false when everything matches', async () => { + const srcPath = path.join(tmpDir, 'matching-source.js'); + await writeFile(srcPath, 'console.log("matched");'); + const outputPath = path.join(tmpDir, 'matching-output.wasm'); + await writeFile(outputPath, 'fake-wasm-content'); + + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const checksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData(buildDataPath, checksum, '1.0.0', '--debug', 'wit-xyz'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '--debug', + 'wit-xyz', + ); + expect(result).to.be.false; + }); + + it('should return true when source content changes', async () => { + const srcPath = path.join(tmpDir, 'changed-source.js'); + await writeFile(srcPath, 'original content'); + const outputPath = path.join(tmpDir, 'changed-output.wasm'); + await writeFile(outputPath, 'fake-wasm'); + + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const oldChecksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData( + buildDataPath, + oldChecksum, + '1.0.0', + '', + 'wit-hash', + ); + + // Now change the source + await writeFile(srcPath, 'modified content'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '', + 'wit-hash', + ); + expect(result).to.be.true; + }); + + it('should return true when componentize version changes', async () => { + const srcPath = path.join(tmpDir, 'version-source.js'); + await writeFile(srcPath, 'version test'); + const outputPath = path.join(tmpDir, 'version-output.wasm'); + await writeFile(outputPath, 'fake-wasm'); + + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const checksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData(buildDataPath, checksum, '1.0.0', '', 'wit-hash'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '2.0.0', // different version + '', + 'wit-hash', + ); + expect(result).to.be.true; + }); + + it('should return true when runtimeArgs changes', async () => { + const srcPath = path.join(tmpDir, 'args-source.js'); + await writeFile(srcPath, 'args test'); + const outputPath = path.join(tmpDir, 'args-output.wasm'); + await writeFile(outputPath, 'fake-wasm'); + + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const checksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData(buildDataPath, checksum, '1.0.0', '', 'wit-hash'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '--enable-script-debugging', // different args + 'wit-hash', + ); + expect(result).to.be.true; + }); + + it('should return true when targetWitChecksum changes', async () => { + const srcPath = path.join(tmpDir, 'wit-source.js'); + await writeFile(srcPath, 'wit test'); + const outputPath = path.join(tmpDir, 'wit-output.wasm'); + await writeFile(outputPath, 'fake-wasm'); + + const { calculateChecksum } = await import('../dist/utils.js'); + const { saveBuildData } = await import('../dist/utils.js'); + const checksum = await calculateChecksum( + await fs.promises.readFile(srcPath), + ); + const buildDataPath = `${srcPath}.buildData.json`; + await saveBuildData(buildDataPath, checksum, '1.0.0', '', 'old-wit-hash'); + + const result = await ShouldComponentize( + srcPath, + outputPath, + '1.0.0', + '', + 'new-wit-hash', // different wit hash + ); + expect(result).to.be.true; + }); +});