This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration.test.ts
154 lines (129 loc) · 5.33 KB
/
integration.test.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { test } from "node:test";
import * as assert from "node:assert";
import { run, runCapture } from "../src/lib/process";
import { randomUUID } from "node:crypto";
test("Project is deployed", async () => {
const componentOneMeta = await getComponentMeta("component-one");
console.log(componentOneMeta);
assert.ok(componentOneMeta);
assert.ok(componentOneMeta["componentUrn"]);
const componentTwoMeta = await getComponentMeta("component-two");
console.log(componentTwoMeta);
assert.ok(componentTwoMeta);
assert.ok(componentTwoMeta["componentUrn"]);
const componentThreeMeta = await getComponentMeta("component-three");
console.log(componentThreeMeta);
assert.ok(componentThreeMeta);
assert.ok(componentThreeMeta["componentUrn"]);
});
test("Calling add on component one calls other components", async () => {
// Setup
const workerName = randomUUID();
console.log(`Random worker name: ${workerName}`);
const componentURNs = await getComponentURNs();
console.log("Component URNs:", componentURNs);
await addWorker("component-one", workerName, componentURNs);
await addWorker("component-two", workerName, componentURNs);
// Check initial counter values
assert.equal(await invokeWorkerGet("component-one", workerName), 0);
assert.equal(await invokeWorkerGet("component-two", workerName), 0);
assert.equal(await invokeWorkerGet("component-three", workerName), 0);
// Call add on component-one and check counter values
await invokeWorkerAdd("component-one", workerName, 2);
assert.equal(await invokeWorkerGet("component-one", workerName), 2);
assert.equal(await invokeWorkerGet("component-two", workerName), 2);
assert.equal(await invokeWorkerGet("component-three", workerName), 4);
// Call add on component-two and check counter values
await invokeWorkerAdd("component-two", workerName, 3);
assert.equal(await invokeWorkerGet("component-one", workerName), 2);
assert.equal(await invokeWorkerGet("component-two", workerName), 5);
assert.equal(await invokeWorkerGet("component-three", workerName), 7);
// Call add on component-three and check counter values
await invokeWorkerAdd("component-three", workerName, 1);
assert.equal(await invokeWorkerGet("component-one", workerName), 2);
assert.equal(await invokeWorkerGet("component-two", workerName), 5);
assert.equal(await invokeWorkerGet("component-three", workerName), 8);
// Call add on component-one and check counter values
await invokeWorkerAdd("component-one", workerName, 1);
assert.equal(await invokeWorkerGet("component-one", workerName), 3);
assert.equal(await invokeWorkerGet("component-two", workerName), 6);
assert.equal(await invokeWorkerGet("component-three", workerName), 10);
});
async function getComponentMeta(compName: string) {
const result = await runCapture("golem-cli", ["--format", "json", "component", "get", "--component-name", compName]);
if (result.code !== 0) {
process.stdout.write(result.stdout);
process.stderr.write(result.stderr);
throw new Error(`component get for ${compName} failed with code ${result.code}`);
}
return JSON.parse(result.stdout);
}
interface ComponentURNs {
componentOne: string;
componentTwo: string;
componentThree: string;
}
async function getComponentURNs(): Promise<ComponentURNs> {
return {
componentOne: (await getComponentMeta("component-one"))["componentUrn"],
componentTwo: (await getComponentMeta("component-two"))["componentUrn"],
componentThree: (await getComponentMeta("component-three"))["componentUrn"],
};
}
function componentIdFromURN(compURN: string) {
return compURN.split(":")[2] as string;
}
async function addWorker(compName: string, workerName: string, componentURNs: ComponentURNs) {
console.log(`Adding worker: ${compName}, ${workerName}`);
return run("golem-cli", [
"worker",
"--format",
"json",
"add",
"--component-name",
compName,
"--worker-name",
workerName,
"--env",
`COMPONENT_ONE_ID=${componentIdFromURN(componentURNs.componentOne)}`,
"--env",
`COMPONENT_TWO_ID=${componentIdFromURN(componentURNs.componentTwo)}`,
"--env",
`COMPONENT_THREE_ID=${componentIdFromURN(componentURNs.componentThree)}`,
]);
}
async function invokeAndAwaitWorker(
compName: string,
workerName: string,
functionName: string,
functionArgs: string[],
) {
console.log(`Invoking worker: ${compName}, ${workerName}, ${functionName}, ${functionArgs}`);
const result = await runCapture("golem-cli", [
"--format",
"json",
"worker",
"invoke-and-await",
"--component-name",
compName,
"--worker-name",
workerName,
"--function",
functionName,
...functionArgs.flatMap((arg) => ["--arg", arg]),
]);
if (result.code !== 0) {
process.stdout.write(result.stdout);
process.stderr.write(result.stderr);
throw new Error(`invoke and await worker failed with code ${result.code}`);
}
console.log(result.stdout);
return JSON.parse(result.stdout);
}
async function invokeWorkerGet(compName: string, workerName: string) {
const result = await invokeAndAwaitWorker(compName, workerName, `golem:${compName}/${compName}-api.{get}`, []);
return result["value"][0] as number;
}
async function invokeWorkerAdd(compName: string, workerName: string, value: number) {
await invokeAndAwaitWorker(compName, workerName, `golem:${compName}/${compName}-api.{add}`, [value.toString()]);
}