Skip to content

Commit

Permalink
Adding tests and the hello service
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeesha committed Dec 16, 2023
1 parent 53a2bf4 commit dfe43b2
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 23 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Run Tests

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 20

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm run test
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<p align="center">
<img src="./assets/logo.png" width="200" style="border-radius: 10px" />
</p>

# Differential Code Examples

## The simplest example

To see the simplest example for a service and the service consumer, see:

1. Hello service at [src/services/hello-service.ts](./src/services/hello-service.ts)
2. Service consumer at [src/example-1-simple.ts](./src/example-1-simple.ts)

To run the example:

### Start the server

```bash
npm run services:hello
```

### Start the client

```bash
npm run client:hello
```
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
"name": "minimal-example",
"version": "1.0.0",
"description": "",
"scripts": {},
"scripts": {
"services:hello": "tsx src/services/hello-service.ts",
"client:hello": "tsx src/example-1-simple.ts",
"test": "DEBUG=* node --import tsx --test src/tests/example.test.ts"
},
"author": "",
"license": "MIT",
"dependencies": {
"@differentialhq/core": "^3.0.3",
"@differentialhq/core": "^3.0.4",
"tsx": "^4.6.2"
},
"devDependencies": {
"@types/node": "^20.10.4"
}
}
}
9 changes: 8 additions & 1 deletion src/example-1-simple.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { d } from "./d";
import type { helloService } from "./services/hello-world";
import type { helloService } from "./services/hello-service";
import process from "process";

(async function () {
console.log("calling hello service");

// this .call is typesafe because of the generics. and you can
// pass in any serializable arguments you want, and accept any
// serializable return value you want.
//
// this function will be served by the helloService
const greeting = await d.call<typeof helloService, "hello">(
"hello",
`process ${process.pid}`
Expand Down
26 changes: 26 additions & 0 deletions src/services/hello-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import process from "process";
import { d } from "../d";

export const hello = async (from: string) => {
console.log("hello function called", from);
return `Hello, ${from}! I'm running on ${process.platform} and on pid=${process.pid}.`;
};

export const helloService = d.service({
name: "hello",
functions: {
hello,
},
});

// this registers a service at api.differential.dev
// under the api secret, and makes it available for
// any d.call() function which uses the same api secret
helloService.start();

console.log("hello service started");

process.on("beforeExit", async () => {
console.log("stopping hello service");
await helloService.stop();
});
15 changes: 0 additions & 15 deletions src/services/hello-world.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/tests/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import test from "node:test";
import { helloService } from "../services/hello-service";
import { d } from "../d";
import assert from "node:assert";

test("example", async (t) => {
// start the service
helloService.start();

const greeting = await d.call<typeof helloService, "hello">(
"hello",
`process ${process.pid}`
);

assert.match(
greeting,
/Hello, process \d+! I'm running on [a-z]+ and on pid=\d+/
);

console.log("stopping service");

// why the fuck is this service not stopping?

// stop the service
await helloService.stop();

console.log("stopped service");
});

0 comments on commit dfe43b2

Please sign in to comment.