// Copyright (c) 2025 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 import * as wasi from 'node:wasi'; import { strictEqual, deepStrictEqual, throws } from 'node:assert'; export const wasiTest = { test() { // Note: test was generated by claude with manual tweaks // Test that WASI class exists and is a constructor strictEqual(typeof wasi.WASI, 'function'); // Test constructor with default options - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { new wasi.WASI(); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test constructor with minimal valid options - should throw ERR_METHOD_NOT_IMPLEMENTED throws( () => { new wasi.WASI({ version: 'unstable' }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'preview1' }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test constructor argument validation - options must be an object throws( () => { new wasi.WASI('invalid'); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI(null); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI(123); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test version validation - must be 'unstable' or 'preview1' throws( () => { new wasi.WASI({ version: 'invalid' }); }, { code: 'ERR_INVALID_ARG_VALUE', } ); throws( () => { new wasi.WASI({ version: 'v1' }); }, { code: 'ERR_INVALID_ARG_VALUE', } ); throws( () => { new wasi.WASI({ version: null }); }, { code: 'ERR_INVALID_ARG_VALUE', } ); throws( () => { new wasi.WASI({ version: 123 }); }, { code: 'ERR_INVALID_ARG_VALUE', } ); // Test args validation - must be an array if provided throws( () => { new wasi.WASI({ version: 'unstable', args: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', args: {} }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', args: 123 }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid args - should still throw ERR_METHOD_NOT_IMPLEMENTED but after validation throws( () => { new wasi.WASI({ version: 'unstable', args: [] }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', args: ['arg1', 'arg2'] }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test env validation - must be an object if provided throws( () => { new wasi.WASI({ version: 'unstable', env: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', env: [] }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', env: 123 }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid env - should still throw ERR_METHOD_NOT_IMPLEMENTED but after validation throws( () => { new wasi.WASI({ version: 'unstable', env: {} }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', env: { PATH: '/usr/bin' } }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test preopens validation - must be an object if provided throws( () => { new wasi.WASI({ version: 'unstable', preopens: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', preopens: [] }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', preopens: 123 }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid preopens - should still throw ERR_METHOD_NOT_IMPLEMENTED but after validation throws( () => { new wasi.WASI({ version: 'unstable', preopens: {} }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', preopens: { '/': '.' } }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test stdin validation - must be a valid int32 >= 0 throws( () => { new wasi.WASI({ version: 'unstable', stdin: -1 }); }, { code: 'ERR_OUT_OF_RANGE', } ); throws( () => { new wasi.WASI({ version: 'unstable', stdin: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', stdin: 2147483648 }); // > MAX_INT32 }, { code: 'ERR_OUT_OF_RANGE', } ); // Test valid stdin values - should still throw ERR_METHOD_NOT_IMPLEMENTED but after validation throws( () => { new wasi.WASI({ version: 'unstable', stdin: 0 }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', stdin: 3 }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test stdout validation - must be a valid int32 >= 0 throws( () => { new wasi.WASI({ version: 'unstable', stdout: -1 }); }, { code: 'ERR_OUT_OF_RANGE', } ); throws( () => { new wasi.WASI({ version: 'unstable', stdout: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid stdout values throws( () => { new wasi.WASI({ version: 'unstable', stdout: 1 }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test stderr validation - must be a valid int32 >= 0 throws( () => { new wasi.WASI({ version: 'unstable', stderr: -1 }); }, { code: 'ERR_OUT_OF_RANGE', } ); throws( () => { new wasi.WASI({ version: 'unstable', stderr: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid stderr values throws( () => { new wasi.WASI({ version: 'unstable', stderr: 2 }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test returnOnExit validation - must be a boolean if provided throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: 'invalid' }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: 1 }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: {} }); }, { code: 'ERR_INVALID_ARG_TYPE', } ); // Test valid returnOnExit values throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: true }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: false }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test complex valid options combination throws( () => { new wasi.WASI({ version: 'preview1', args: ['node', 'script.js'], env: { NODE_ENV: 'production' }, preopens: { '/tmp': '/tmp' }, stdin: 0, stdout: 1, stderr: 2, returnOnExit: true, }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test that getImportObject can be called on prototype (even though we can't instantiate) strictEqual(typeof wasi.WASI.prototype.getImportObject, 'function'); // Test getImportObject returns empty object const importObject = wasi.WASI.prototype.getImportObject.call({}); strictEqual(typeof importObject, 'object'); deepStrictEqual(importObject, {}); // Test that other methods exist on prototype strictEqual(typeof wasi.WASI.prototype.finalizeBindings, 'function'); strictEqual(typeof wasi.WASI.prototype.start, 'function'); strictEqual(typeof wasi.WASI.prototype.initialize, 'function'); // Test that other methods throw ERR_METHOD_NOT_IMPLEMENTED when called on prototype throws( () => { wasi.WASI.prototype.finalizeBindings.call({}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { wasi.WASI.prototype.start.call({}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { wasi.WASI.prototype.initialize.call({}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test finalizeBindings with various parameters throws( () => { wasi.WASI.prototype.finalizeBindings.call({}, {}, {}); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { wasi.WASI.prototype.finalizeBindings.call({}, {}, { memory: null }); }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); // Test default export strictEqual(typeof wasi.default, 'object'); strictEqual(wasi.default.WASI, wasi.WASI); // Test edge cases with undefined/null in options throws( () => { new wasi.WASI({ version: 'unstable', args: undefined }); // undefined should be ok }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', env: undefined }); // undefined should be ok }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', preopens: undefined }); // undefined should be ok }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); throws( () => { new wasi.WASI({ version: 'unstable', returnOnExit: undefined }); // undefined should be ok }, { code: 'ERR_METHOD_NOT_IMPLEMENTED', } ); console.log('All wasi module tests passed!'); }, };