feat: initial commit
This commit is contained in:
7
misc/client-sdk-testsuite/src/arcad.yml
Normal file
7
misc/client-sdk-testsuite/src/arcad.yml
Normal file
@ -0,0 +1,7 @@
|
||||
---
|
||||
id: app.arcad.edge.client-sdk-test
|
||||
title: SDK Test
|
||||
version: 0.0.0
|
||||
description: |
|
||||
Suite de tests pour le SDK client
|
||||
tags: ["test"]
|
28
misc/client-sdk-testsuite/src/public/index.html
Normal file
28
misc/client-sdk-testsuite/src/public/index.html
Normal 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="/client.js"></script>
|
||||
<script src="test/client-sdk.js"></script>
|
||||
<script class="mocha-exec">
|
||||
mocha.run();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
147
misc/client-sdk-testsuite/src/public/test/client-sdk.js
Normal file
147
misc/client-sdk-testsuite/src/public/test/client-sdk.js
Normal file
@ -0,0 +1,147 @@
|
||||
Arcad.debug = true;
|
||||
|
||||
describe('Arcad', function() {
|
||||
|
||||
describe('#connect()', function() {
|
||||
after(() => {
|
||||
Arcad.disconnect();
|
||||
});
|
||||
|
||||
it('should open the connection', function() {
|
||||
return Arcad.connect()
|
||||
.then(() => {
|
||||
chai.assert.isNotNull(Arcad._conn);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#disconnect()', function() {
|
||||
it('should close the connection', function() {
|
||||
Arcad.disconnect();
|
||||
chai.assert.isNull(Arcad._conn);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#send()', function() {
|
||||
this.timeout(5000);
|
||||
|
||||
before(() => {
|
||||
return Arcad.connect();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
Arcad.disconnect();
|
||||
});
|
||||
|
||||
it('should send a message to the backend and echo back', function(done) {
|
||||
const now = new Date();
|
||||
const handler = evt => {
|
||||
chai.assert.equal(evt.detail.now, now.toJSON());
|
||||
Arcad.removeEventListener('message', handler);
|
||||
done();
|
||||
}
|
||||
|
||||
// Server should echo back message
|
||||
Arcad.addEventListener('message', handler);
|
||||
|
||||
// Send message to backend
|
||||
Arcad.send({ now });
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Remote Procedure Call', function() {
|
||||
|
||||
before(() => {
|
||||
return Arcad.connect();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
Arcad.disconnect();
|
||||
});
|
||||
|
||||
it('should call the remote echo() method and resolve the returned value', function() {
|
||||
const foo = "bar";
|
||||
|
||||
return Arcad.rpc('echo', { foo })
|
||||
.then(result => {
|
||||
chai.assert.equal(result.foo, foo);
|
||||
});
|
||||
});
|
||||
|
||||
it('should call the remote throwError() method and reject with an error', function() {
|
||||
return Arcad.rpc('throwError')
|
||||
.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 Arcad.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(5000);
|
||||
|
||||
const values = [];
|
||||
for(let i = 0; i <= 1000; i++) {
|
||||
values.push((Math.random() * 1000 | 0));
|
||||
}
|
||||
return Arcad.rpc('reset')
|
||||
.then(() => {
|
||||
return Promise.all(values.map(v => Arcad.rpc("add", {value: v})));
|
||||
})
|
||||
.then(() => Arcad.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 Arcad.connect();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
Arcad.disconnect();
|
||||
});
|
||||
|
||||
it('should upload then download a file', function() {
|
||||
const content = "file "+(new Date()).toJSON();
|
||||
const file = new Blob([content], {type: "text/plain"});
|
||||
|
||||
return Arcad.upload(file)
|
||||
.then(upload => upload.result())
|
||||
.then(fileId => {
|
||||
|
||||
chai.assert.isNotEmpty(fileId);
|
||||
|
||||
const fileUrl = Arcad.fileUrl(fileId);
|
||||
chai.assert.isNotEmpty(fileUrl);
|
||||
|
||||
return fetch(fileUrl)
|
||||
.then(res => res.text())
|
||||
.then(fileContent => {
|
||||
chai.assert.equal(content, fileContent);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
chai.assert.fail(err);
|
||||
})
|
||||
});
|
||||
|
||||
});
|
70
misc/client-sdk-testsuite/src/server/main.js
Normal file
70
misc/client-sdk-testsuite/src/server/main.js
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
// Called on backend initialization
|
||||
function onInit() {
|
||||
console.log("backend started");
|
||||
|
||||
// Register RPC exposed methods
|
||||
rpc.register("echo");
|
||||
rpc.register("throwError");
|
||||
|
||||
rpc.register("add");
|
||||
rpc.register("reset");
|
||||
rpc.register("total");
|
||||
}
|
||||
|
||||
// Called for each user connection
|
||||
function onUserConnect() {
|
||||
console.log("onUserConnect")
|
||||
}
|
||||
|
||||
// Called for each user disconnection
|
||||
function onUserDisconnect() {
|
||||
console.log("onUserDisconnect")
|
||||
}
|
||||
|
||||
// Called for each user message
|
||||
function onUserMessage(data) {
|
||||
console.log("onUserMessage", data.now);
|
||||
net.send({ now: data.now });
|
||||
}
|
||||
|
||||
// Called for each file upload request
|
||||
function onFileUpload(fileId, fileInfo) {
|
||||
console.log("onFileUpload", fileId, fileInfo);
|
||||
if (fileInfo.contentType !== "text/plain") return false;
|
||||
if (fileInfo.filename !== 'blob') return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Called for each file download request
|
||||
function onFileDownload(fileId) {
|
||||
console.log("onFileDownload", fileId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// RPC methods
|
||||
|
||||
function echo(params) {
|
||||
console.log("echoing", params);
|
||||
return params;
|
||||
}
|
||||
|
||||
function throwError() {
|
||||
throw new Error("oh no !");
|
||||
}
|
||||
|
||||
var count = 0;
|
||||
|
||||
function add(params) {
|
||||
console.log("add", params);
|
||||
count += params.value;
|
||||
return count;
|
||||
}
|
||||
|
||||
function reset() {
|
||||
count = 0;
|
||||
}
|
||||
|
||||
function total() {
|
||||
return count;
|
||||
}
|
Reference in New Issue
Block a user