feat: initial commit

This commit is contained in:
2023-02-09 12:16:36 +01:00
commit 81dc1adfef
126 changed files with 11551 additions and 0 deletions

View File

@ -0,0 +1,7 @@
---
id: edge.sdk.client.test
title: SDK Test
version: 0.0.0
description: |
Suite de tests pour le SDK client
tags: ["test"]

View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Client SDK Test suite</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="vendor/mocha.css" />
<style>
body {
background-color: white;
}
</style>
</head>
<body>
<div id="mocha"></div>
<script src="vendor/chai.js"></script>
<script src="vendor/mocha.js"></script>
<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
</script>
<script src="/edge/sdk/client.js"></script>
<script src="test/client-sdk.js"></script>
<script class="mocha-exec">
mocha.run();
</script>
</body>
</html>

View File

@ -0,0 +1,148 @@
Edge.debug = true;
describe('Edge', function() {
describe('#connect()', function() {
after(() => {
Edge.disconnect();
});
it('should open the connection', function() {
return Edge.connect()
.then(() => {
chai.assert.isNotNull(Edge._conn);
});
});
});
describe('#disconnect()', function() {
it('should close the connection', function() {
Edge.disconnect();
chai.assert.isNull(Edge._conn);
});
});
describe('#send()', function() {
this.timeout(5000);
before(() => {
return Edge.connect();
});
after(() => {
Edge.disconnect();
});
it('should send a message to the server and echo back', function(done) {
const now = new Date();
const handler = evt => {
chai.assert.equal(evt.detail.now, now.toJSON());
Edge.removeEventListener('message', handler);
done();
}
// Server should echo back message
Edge.addEventListener('message', handler);
// Send message to server
Edge.send({ now });
});
});
});
describe('Remote Procedure Call', function() {
before(() => {
return Edge.connect();
});
after(() => {
Edge.disconnect();
});
it('should call the remote echo() method and resolve the returned value', function() {
const foo = "bar";
return Edge.rpc('echo', { foo })
.then(result => {
chai.assert.equal(result.foo, foo);
});
});
it('should call the remote throwErrorFromClient() method and reject with an error', function() {
return Edge.rpc('throwErrorFromClient')
.catch(err => {
// Assert that it's an "internal" error
// See https://www.jsonrpc.org/specification#error_object
chai.assert.equal(err.code, -32603);
});
});
it('should call an unregistered method and reject with an error', function() {
return Edge.rpc('unregisteredMethod')
.catch(err => {
// Assert that it's an "method not found" error
// See https://www.jsonrpc.org/specification#error_object
chai.assert.equal(err.code, -32601);
});
});
it('should call the add() method repetitively and keep count of the sent values', function() {
this.timeout(10000);
const values = [];
for(let i = 0; i <= 1000; i++) {
values.push((Math.random() * 1000 | 0));
}
return Edge.rpc('reset')
.then(() => {
return Promise.all(values.map(v => Edge.rpc("add", {value: v})));
})
.then(() => Edge.rpc('total'))
.then(remoteTotal => {
const localTotal = values.reduce((t, v) => t+v);
console.log("Remote total:", remoteTotal, "Local total:", localTotal);
chai.assert.equal(remoteTotal, localTotal)
})
});
});
describe('File Module', function() {
before(() => {
return Edge.connect();
});
after(() => {
Edge.disconnect();
});
it('should upload then download a blob', function() {
const content = JSON.stringify({"date": new Date()});
const blob = new Blob([content], {type: "application/json"});
return Edge.upload(blob)
.then(upload => upload.result())
.then(result => {
chai.assert.isNotEmpty(result.blobId);
chai.assert.isNotEmpty(result.bucket);
const blobUrl = Edge.blobUrl(result.bucket, result.blobId);
chai.assert.isNotEmpty(blobUrl);
return fetch(blobUrl)
.then(res => res.text())
.then(blobContent => {
chai.assert.equal(content, blobContent);
});
})
.catch(err => {
chai.assert.fail(err);
})
});
});

View File

@ -0,0 +1,63 @@
// Called on server initialization
function onInit() {
console.log("server started");
// Register RPC exposed methods
rpc.register("echo", echo);
rpc.register("throwErrorFromClient", throwError);
rpc.register("add", add);
rpc.register("reset", reset);
rpc.register("total", total);
}
// Called for each client message
function onClientMessage(ctx, data) {
var sessionId = context.get(ctx, context.SESSION_ID);
console.log("onClientMessage", sessionId, data.now);
net.send(ctx, { now: data.now });
}
// Called for each blob upload request
function onBlobUpload(ctx, blobId, blobInfo, metadata) {
console.log("onBlobUpload", blobId, blobInfo, metadata);
if (!blobInfo.contentType == "application/json") return { allow: false };
if (!blobInfo.filename == "blob") return { allow: false };
return { allow: true, bucket: "test-bucket" };
}
// Called for each blob download request
function onBlobDownload(ctx, bucket, blobId) {
console.log("onBlobDownload", bucket, blobId);
return { allow: true };
}
// RPC methods
function echo(ctx, params) {
console.log("echoing", params);
return params;
}
function throwError(ctx, params) {
throw new Error("oh no !");
}
var count = 0;
function add(ctx, params) {
console.log("add", params);
count += params.value;
return count;
}
function reset(ctx, params) {
count = 0;
}
function total(ctx, params) {
return count;
}