-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
70 lines (55 loc) · 1.83 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require("fs");
const assert = require("assert");
const { getPatients, postToInbox, isInboxAvailable } = require("./clients");
function fixture(path) {
return fs.readFileSync(`fixtures/${path}`, "utf8");
}
function assertTrigger(response, trigger) {
assert.strictEqual(
response.data.data[0],
trigger,
`Payload didn't match the ${trigger} trigger`
);
}
function expectOnePatient(response) {
assert.strictEqual(response.data.total, 1, "Expected exactly one patient.");
}
const pRetry = require("p-retry");
async function expectOnePatientWith(identifier) {
async function getPatient() {
const response = await getPatients(identifier);
assert.strictEqual(response.data.total, 1, `Expected exactly one patient with ${identifier}.`);
return true;
}
return await pRetry(getPatient, {
onFailedAttempt: (error) => {
if (!(error instanceof assert.AssertionError)) {
throw error
}
console.log(
`Patient not there yet, trying again... ${error.attemptNumber}/10`
);
},
retries: 10,
});
}
(async function () {
console.log("Waiting for Microservice to be available...");
await isInboxAvailable();
let response = await postToInbox(fixture("organization.json"));
assertTrigger(response, "create-organization")
response = await postToInbox(fixture("commcare_sample.json"));
assertTrigger(response, "commcare-to-openhim")
response = await postToInbox(fixture("koboCaseRegistration.json"));
assertTrigger(response, "kobo-to-openhim")
await expectOnePatientWith({ identifier: "C19-930020123" });
await expectOnePatientWith({ identifier: "test_clinic-0009-01" });
console.log("👍 Everything checks out.");
})().catch((e) => {
if (e instanceof assert.AssertionError) {
console.error(e.message);
process.exitCode = 10;
} else {
throw e;
}
});