Skip to content

Commit

Permalink
feat: Add support for browser contract tests. (#582)
Browse files Browse the repository at this point in the history
This adds the contract tests, but does not yet automate running them.
  • Loading branch information
kinyoklion authored Sep 16, 2024
1 parent 0848ab7 commit 38f081e
Show file tree
Hide file tree
Showing 27 changed files with 1,001 additions and 14 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"packages/store/node-server-sdk-dynamodb",
"packages/telemetry/node-server-sdk-otel",
"packages/tooling/jest",
"packages/sdk/browser"
"packages/sdk/browser",
"packages/sdk/browser/contract-tests/entity",
"packages/sdk/browser/contract-tests/adapter"
],
"private": true,
"scripts": {
Expand Down
40 changes: 40 additions & 0 deletions packages/sdk/browser/contract-tests/adapter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "browser-contract-test-adapter",
"version": "1.0.0",
"description": "Adapts REST interface to a websocket for use in browsers.",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "yarn build && node dist/index.js",
"lint": "eslint ./src",
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../../.prettierignore"
},
"author": "",
"license": "UNLICENSED",
"dependencies": {
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"express": "^4.21.0",
"ws": "^8.18.0"
},
"devDependencies": {
"@eslint/js": "^9.10.0",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.45.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-prettier": "^5.0.0",
"globals": "^15.9.0",
"prettier": "^3.0.0",
"typescript": "^5.6.2",
"typescript-eslint": "^8.5.0"
}
}
112 changes: 112 additions & 0 deletions packages/sdk/browser/contract-tests/adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable no-console */

/* eslint-disable @typescript-eslint/no-explicit-any */
import bodyParser from 'body-parser';
import cors from 'cors';
import { randomUUID } from 'crypto';
import express from 'express';
import http from 'node:http';
import util from 'node:util';
import { WebSocketServer } from 'ws';

let server: http.Server | undefined;

async function main() {
const wss = new WebSocketServer({ port: 8001 });
const waiters: Record<string, (data: unknown) => void> = {};

console.log('Running contract test harness adapter.');
wss.on('connection', async (ws) => {
ws.on('error', console.error);

ws.on('message', (stringData: string) => {
const data = JSON.parse(stringData);
if (Object.prototype.hasOwnProperty.call(waiters, data.reqId)) {
waiters[data.reqId](data);
delete waiters[data.reqId];
} else {
console.error('Did not find outstanding request', data.reqId);
}
});

const send = (data: { [key: string]: unknown; reqId: string }): Promise<any> => {
let resolver: (data: unknown) => void;
const waiter = new Promise((resolve) => {
resolver = resolve;
});
// @ts-expect-error The body of the above assignment runs sequentially.
waiters[data.reqId] = resolver;
ws.send(JSON.stringify(data));
return waiter;
};

if (server) {
await util.promisify(server.close).call(server);
server = undefined;
}

const app = express();

const port = 8000;

app.use(
cors({
origin: '*',
allowedHeaders: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
}),
);
app.use(bodyParser.json());

app.get('/', async (_req, res) => {
const commandResult = await send({ command: 'getCapabilities', reqId: randomUUID() });
res.header('Content-Type', 'application/json');
res.json(commandResult);
});

app.delete('/', () => {
process.exit();
});

app.post('/', async (req, res) => {
const commandResult = await send({
command: 'createClient',
body: req.body,
reqId: randomUUID(),
});
if (commandResult.resourceUrl) {
res.set('Location', commandResult.resourceUrl);
}
if (commandResult.status) {
res.status(commandResult.status);
}
res.send();
});

app.post('/clients/:id', async (req, res) => {
const commandResult = await send({
command: 'runCommand',
id: req.params.id,
body: req.body,
reqId: randomUUID(),
});
if (commandResult.status) {
res.status(commandResult.status);
}
if (commandResult.body) {
res.write(JSON.stringify(commandResult.body));
}
res.send();
});

app.delete('/clients/:id', async (req, res) => {
await send({ command: 'deleteClient', id: req.params.id, reqId: randomUUID() });
res.send();
});

server = app.listen(port, () => {
console.log('Listening on port %d', port);
});
});
}
main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["/**/*.ts"],
"exclude": ["node_modules"]
}
14 changes: 14 additions & 0 deletions packages/sdk/browser/contract-tests/adapter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true
},
"lib": ["ES6"],
"exclude": ["**/*.test.ts", "dist", "node_modules"]
}
7 changes: 7 additions & 0 deletions packages/sdk/browser/contract-tests/adapter/tsconfig.ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*", "package.json"],
"compilerOptions": {
"composite": true
}
}
13 changes: 13 additions & 0 deletions packages/sdk/browser/contract-tests/entity/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SDK Contract Test Service</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions packages/sdk/browser/contract-tests/entity/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "browser-contract-test-service",
"private": true,
"version": "0.0.0",
"type": "module",
"description": "Contract test service implementation for @launchdarkly/js-client-sdk",
"scripts": {
"start": "vite --open=true",
"lint": "eslint ./src",
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../../.prettierignore"
},
"dependencies": {
"@launchdarkly/js-client-sdk": "0.0.0"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.45.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.0",
"typescript": "^5.5.3",
"vite": "^5.4.1"
}
}
Loading

0 comments on commit 38f081e

Please sign in to comment.