Skip to content

Commit 18bec6a

Browse files
committed
Enhance testing setup by adding demo HTML and JS files, and update package.json scripts for automated testing
1 parent bf99fda commit 18bec6a

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
sqlite-wasm/*
33
!sqlite-wasm/package.json
4-
!sqlite-wasm/README.md
4+
!sqlite-wasm/README.md
5+
!sqlite-wasm/test

sqlite-wasm/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
"clean": "cd .. && git clean -fdX",
4848
"build": "npm run clean && cd .. && ./build.sh",
4949
"start": "npx http-server --coop",
50+
"test": "npm run start & HTTP_SERVER_PID=$! && sleep 3 && node test/test.cjs && kill $HTTP_SERVER_PID",
5051
"fix": "npx prettier . --write",
51-
"prepublishOnly": "npm run build && npm run fix && npm run publint && npm run check-types",
52-
"deploy": "npm run prepublishOnly && npm publish --access public --provenance"
52+
"prepare": "npm run build && npm run fix && npm run publint && npm run check-types npm run test",
53+
"deploy": "npm run prepare && npm publish --access public --provenance"
5354
},
5455
"repository": {
5556
"type": "git",
@@ -63,6 +64,7 @@
6364
"homepage": "https://github.com/sqliteai/sqlite-wasm#readme",
6465
"devDependencies": {
6566
"http-server": "github:vapier/http-server",
67+
"playwright": "*",
6668
"prettier": "^3.5.3",
6769
"prettier-plugin-jsdoc": "^1.3.2",
6870
"publint": "^0.3.12",

sqlite-wasm/test/demo.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<body>
4+
<script>(function(){
5+
const logHtml = function(cssClass,...args){
6+
const ln = document.createElement('div');
7+
if(cssClass) ln.classList.add(cssClass);
8+
ln.append(document.createTextNode(args.join(' ')));
9+
document.body.append(ln);
10+
};
11+
const w = new Worker("demo.js?sqlite3.dir=../sqlite-wasm/jswasm");
12+
w.onmessage = function({data}){
13+
switch(data.type){
14+
case 'log':
15+
logHtml(data.payload.cssClass, ...data.payload.args);
16+
break;
17+
default:
18+
logHtml('error',"Unhandled message:",data.type);
19+
};
20+
};
21+
})();</script>
22+
</body>
23+
</html>

sqlite-wasm/test/demo.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
(function(){
3+
const logHtml = function(cssClass,...args){
4+
postMessage({
5+
type:'log',
6+
payload:{cssClass, args}
7+
});
8+
}
9+
const log = (...args)=>logHtml('',...args);
10+
const error = (...args)=>logHtml('error',...args);
11+
12+
const testVersion = function(sqlite3){
13+
const db = new sqlite3.oo1.DB("/mydb.sqlite3",'ct');
14+
15+
try {
16+
let testString = '';
17+
db.exec({
18+
sql: "select sqlite_version();",
19+
rowMode: 'array',
20+
callback: function(version){
21+
testString += version;
22+
}
23+
});
24+
db.exec({
25+
sql: "select cloudsync_version();",
26+
rowMode: 'array',
27+
callback: function(version){
28+
testString += `-sync.${version}`;
29+
}
30+
});
31+
db.exec({
32+
sql: "select vector_version();",
33+
rowMode: 'array',
34+
callback: function(version){
35+
testString += `-vector.${version}`;
36+
}
37+
});
38+
log(testString);
39+
}finally{
40+
db.close();
41+
}
42+
43+
};
44+
45+
if(globalThis.window!==globalThis) {
46+
let sqlite3Js = 'sqlite3.js';
47+
const urlParams = new URL(globalThis.location.href).searchParams;
48+
if(urlParams.has('sqlite3.dir')){
49+
sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js;
50+
}
51+
importScripts(sqlite3Js);
52+
}
53+
globalThis.sqlite3InitModule({
54+
print: log,
55+
printErr: console.error
56+
}).then(function(sqlite3){
57+
try {
58+
testVersion(sqlite3);
59+
}catch(e){
60+
error("Exception:",e.message);
61+
}
62+
});
63+
})();

sqlite-wasm/test/test.cjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { chromium, firefox, webkit } = require('playwright');
2+
const packageJson = require('../package.json');
3+
4+
(async () => {
5+
[
6+
chromium,
7+
firefox,
8+
webkit
9+
].forEach(async (browser) => {
10+
const window = await browser.launch();
11+
const context = await window.newContext();
12+
const page = await context.newPage();
13+
14+
await page.goto('http://127.0.0.1:8081/test/demo.html');
15+
16+
//sleep 5s
17+
await new Promise(r => setTimeout(r, 5000));
18+
19+
const version = await page.evaluate(() => document.body.innerText);
20+
21+
if (version != packageJson.version) throw Error('Error: version mismatch');
22+
console.log(`✅ ${browser.name()} sqlite-wasm test passed`);
23+
24+
await window.close();
25+
});
26+
})();

0 commit comments

Comments
 (0)