Skip to content

Commit

Permalink
add instead of replace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Apr 25, 2024
1 parent 077170e commit 289d96c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
12 changes: 7 additions & 5 deletions packages/cloudflare/test/fixtures/wasm/src/pages/add/[a]/[b].ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { type APIContext } from 'astro';
import { add } from '../../../util/add';
// @ts-ignore
import mod from '../../../util/add.wasm?module';

const addModule: any = new WebAssembly.Instance(mod);

export const prerender = false;

export async function GET(
context: APIContext
): Promise<Response> {
const a = Number.parseInt(context.params.a ?? "0");
const b = Number.parseInt(context.params.b ?? "0");
return new Response(JSON.stringify({ answer: add(a, b) }), {
const a = Number.parseInt(context.params.a!);
const b = Number.parseInt(context.params.b!);
return new Response(JSON.stringify({ answer: addModule.exports.add(a, b) }), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
}
}
10 changes: 5 additions & 5 deletions packages/cloudflare/test/fixtures/wasm/src/pages/hybrid.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { type APIContext } from 'astro';
// @ts-ignore
import mod from '../util/add.wasm?module';

import {add} from '../util/add';

export const prerender = true
const addModule: any = new WebAssembly.Instance(mod);

export async function GET(
context: APIContext
): Promise<Response> {
return new Response(JSON.stringify({ answer: add(20, 1) }), {
return new Response(JSON.stringify({ answer: addModule.exports.add(20, 1) }), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
}
}
16 changes: 16 additions & 0 deletions packages/cloudflare/test/fixtures/wasm/src/pages/hybridshared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type APIContext } from 'astro';

import {add} from '../util/add';

export const prerender = true

export async function GET(
context: APIContext
): Promise<Response> {
return new Response(JSON.stringify({ answer: add(20, 1) }), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
}
18 changes: 18 additions & 0 deletions packages/cloudflare/test/fixtures/wasm/src/pages/shared/[a]/[b].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type APIContext } from 'astro';
import { add } from '../../../util/add';


export const prerender = false;

export async function GET(
context: APIContext
): Promise<Response> {
const a = Number.parseInt(context.params.a ?? "0");
const b = Number.parseInt(context.params.b ?? "0");
return new Response(JSON.stringify({ answer: add(a, b) }), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
}
14 changes: 14 additions & 0 deletions packages/cloudflare/test/wasm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ describe('WasmImport', () => {
const json = await res.json();
assert.deepEqual(json, { answer: 21 });
});

it('can render shared', async () => {
const res = await fetch('http://127.0.0.1:8788/shared/40/2');
assert.equal(res.status, 200);
const json = await res.json();
assert.deepEqual(json, { answer: 42 });
});

it('can render static shared', async () => {
const res = await fetch('http://127.0.0.1:8788/hybridshared');
assert.equal(res.status, 200);
const json = await res.json();
assert.deepEqual(json, { answer: 21 });
});
});

0 comments on commit 289d96c

Please sign in to comment.