2024-04-23 13:14:06 +09:00
|
|
|
import { Buffer } from 'buffer'
|
|
|
|
|
2024-01-08 22:02:46 +09:00
|
|
|
import { describe, it, expect } from 'vitest'
|
|
|
|
|
2024-04-23 13:14:06 +09:00
|
|
|
global.Buffer = Buffer
|
|
|
|
|
2024-01-08 22:02:46 +09:00
|
|
|
// @ts-expect-error
|
|
|
|
const {
|
2024-04-19 17:12:30 +09:00
|
|
|
// @ts-expect-error
|
|
|
|
__fs,
|
2024-01-08 22:02:46 +09:00
|
|
|
DEFAULT_COST,
|
|
|
|
Bird,
|
|
|
|
GetterSetterWithClosures,
|
|
|
|
tsfnReturnPromise,
|
|
|
|
tsfnReturnPromiseTimeout,
|
2024-04-23 13:14:06 +09:00
|
|
|
asyncTaskReadFile,
|
2024-02-26 16:46:07 +09:00
|
|
|
}: typeof import('../index.cjs') = await import('../example.wasi-browser')
|
2024-01-08 22:02:46 +09:00
|
|
|
|
|
|
|
describe('NAPI-RS wasi browser test', function () {
|
|
|
|
it('DEFAULT_COST', function () {
|
|
|
|
expect(DEFAULT_COST).toBe(12)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('async self in class', async function () {
|
|
|
|
const b = new Bird('foo')
|
|
|
|
expect(await b.getNameAsync()).toBe('foo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Class with getter setter closures', () => {
|
|
|
|
const instance = new GetterSetterWithClosures()
|
|
|
|
// @ts-expect-error
|
|
|
|
instance.name = 'Allie'
|
|
|
|
// @ts-expect-error
|
|
|
|
expect(instance.name).toBe(`I'm Allie`)
|
|
|
|
// @ts-expect-error
|
|
|
|
expect(instance.age).toBe(0.3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('threadsafe function return Promise and await in Rust', async () => {
|
|
|
|
const value = await tsfnReturnPromise((err, value) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
return Promise.resolve(value + 2)
|
|
|
|
})
|
|
|
|
expect(value).toBe(5)
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
|
|
expect(
|
|
|
|
tsfnReturnPromiseTimeout((err, value) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(value + 2)
|
|
|
|
}, 300)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
).rejects.toMatchObject(new Error('Timeout'))
|
|
|
|
// trigger Promise.then in Rust after `Promise` is dropped
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 400))
|
|
|
|
})
|
2024-04-19 17:12:30 +09:00
|
|
|
|
2024-04-23 13:14:06 +09:00
|
|
|
it('readFileAsync', async () => {
|
|
|
|
__fs.writeFileSync('/test.txt', 'hello world')
|
|
|
|
const value = await asyncTaskReadFile('/test.txt')
|
|
|
|
expect(value.toString('utf8')).toBe('hello world')
|
2024-04-19 17:12:30 +09:00
|
|
|
})
|
2024-01-08 22:02:46 +09:00
|
|
|
})
|