Skip to content

Commit bd29db4

Browse files
committed
wip(MetaTest): deploy tg(python, ts) from metatest
1 parent 9a770a8 commit bd29db4

File tree

4 files changed

+125
-93
lines changed

4 files changed

+125
-93
lines changed

typegate/tests/runtimes/python_wasi/python_wasi_test.ts

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
import { assert, assertEquals } from "std/assert/mod.ts";
55
import { gql, Meta } from "../../utils/mod.ts";
66
import { PythonVirtualMachine } from "../../../src/runtimes/python_wasi/python_vm.ts";
7-
// import { QueryEngine } from "../../../src/engine/query_engine.ts";
8-
import { BasicAuth, tgDeploy, tgRemove } from "@typegraph/sdk/tg_deploy.js";
97
import { testDir } from "test-utils/dir.ts";
108
import { tg } from "./python_wasi.ts";
119
import * as path from "std/path/mod.ts";
1210

13-
const cwdDir = path.join(testDir, "runtimes/python_wasi");
14-
const auth = new BasicAuth("admin", "password");
11+
const cwd = path.join(testDir, "runtimes/python_wasi");
1512

1613
Meta.test("Python WASI VM performance", async (t) => {
1714
const vm = new PythonVirtualMachine();
@@ -63,36 +60,6 @@ Meta.test("Python WASI VM performance", async (t) => {
6360
await vm.destroy();
6461
});
6562

66-
const deployTypegraph = async (gate: string) => {
67-
const { serialized, typegate: gateResponseAdd } = await tgDeploy(tg, {
68-
baseUrl: gate,
69-
auth,
70-
artifactsConfig: {
71-
prismaMigration: {
72-
globalAction: {
73-
create: true,
74-
reset: false,
75-
},
76-
migrationDir: "prisma-migrations",
77-
},
78-
dir: cwdDir,
79-
},
80-
typegraphPath: path.join(cwdDir, "wasmedge.ts"),
81-
secrets: {},
82-
});
83-
84-
return { serialized, gateResponseAdd };
85-
};
86-
87-
const removeTypegraph = async (gate: string) => {
88-
const { typegate: gateResponseRem } = await tgRemove(tg, {
89-
baseUrl: gate,
90-
auth,
91-
});
92-
93-
return gateResponseRem;
94-
};
95-
9663
// Meta.test("Python WASI runtime", async (t) => {
9764
// const e = await t.engine("runtimes/python_wasi/python_wasi.py");
9865

@@ -346,14 +313,12 @@ Meta.test({
346313
port: true,
347314
systemTypegraphs: true,
348315
}, async (metaTest) => {
349-
const port = metaTest.port;
350-
const gate = `http://localhost:${port}`;
351-
352316
await metaTest.should("upload artifacts along with deps", async () => {
353-
const { serialized, gateResponseAdd: _gateResponseAdd } =
354-
await deployTypegraph(gate);
355-
356-
const engine = await metaTest.engineFromDeployed(serialized);
317+
const engine = await metaTest.engineFromTgDeploy(
318+
"runtimes/python_wasi/python_wasi.ts",
319+
cwd,
320+
tg,
321+
);
357322

358323
await gql`
359324
query {
@@ -373,8 +338,6 @@ Meta.test({
373338
},
374339
})
375340
.on(engine);
376-
377-
await removeTypegraph(gate);
378341
});
379342
});
380343

typegate/tests/runtimes/wasmedge/wasmedge.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import sys
3+
14
from typegraph.gen.exports.core import (
25
ArtifactResolutionConfig,
36
MigrationAction,
@@ -27,22 +30,30 @@ def wasmedge(g: Graph):
2730
)
2831

2932

30-
PORT = 7698
33+
cwd = sys.argv[1]
34+
PORT = sys.argv[2]
3135
gate = f"http://localhost:{PORT}"
3236
auth = BasicAuth("admin", "password")
3337

3438
wasmedge_tg = wasmedge()
35-
serialized, typegate = tg_deploy(
39+
deploy_result = tg_deploy(
3640
wasmedge_tg,
3741
TypegraphDeployParams(
3842
base_url=gate,
43+
auth=auth,
44+
typegraph_path=os.path.join(cwd, "wasmedge.py"),
3945
artifacts_config=ArtifactResolutionConfig(
46+
dir=cwd,
47+
prefix=None,
48+
disable_artifact_resolution=None,
49+
codegen=None,
4050
prisma_migration=MigrationConfig(
4151
migration_dir="prisma-migrations",
4252
global_action=MigrationAction(reset=False, create=True),
43-
)
53+
runtime_actions=None,
54+
),
4455
),
4556
),
4657
)
4758

48-
print(serialized)
59+
print(deploy_result.serialized)
Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,50 @@
11
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
22
// SPDX-License-Identifier: Elastic-2.0
33

4-
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";
54
import { gql, Meta } from "test-utils/mod.ts";
65
import { testDir } from "test-utils/dir.ts";
76
import { tg } from "./wasmedge.ts";
87
import * as path from "std/path/mod.ts";
98

10-
const cwdDir = path.join(testDir, "runtimes/wasmedge");
11-
const auth = new BasicAuth("admin", "password");
12-
13-
// Meta.test("WasmEdge runtime: Python SDK", async (t) => {
14-
// await t.should("works on the Python SDK", async () => {
15-
// const serialized = await t.serializeTypegraphFromShell(
16-
// "runtimes/wasmedge/wasmedge.py",
17-
// SDKLangugage.Python,
18-
// );
19-
20-
// const engine = await t.engineFromDeployed(serialized);
21-
22-
// await gql`
9+
const cwd = path.join(testDir, "runtimes/wasmedge");
10+
11+
// Meta.test(
12+
// {
13+
// name: "WasmEdge runtime: Python SDK",
14+
// port: true,
15+
// systemTypegraphs: true,
16+
// },
17+
// async (t) => {
18+
// await t.should("works on the Python SDK", async () => {
19+
// const engine = await t.engineFromTgDeploy(
20+
// "runtimes/wasmedge/wasmedge.py",
21+
// cwdDir,
22+
// );
23+
24+
// await gql`
2325
// query {
2426
// test(a: 1, b: 2)
2527
// }
2628
// `
27-
// .expectData({
28-
// test: 3,
29-
// })
30-
// .on(engine);
31-
// await engine.terminate();
32-
// });
33-
// }, { port: port });
29+
// .expectData({
30+
// test: 3,
31+
// })
32+
// .on(engine);
33+
// });
34+
// },
35+
// );
3436

3537
Meta.test({
3638
name: "WasmEdge Runtime typescript sdk",
3739
port: true,
3840
systemTypegraphs: true,
3941
}, async (metaTest) => {
40-
const port = metaTest.port;
41-
const gate = `http://localhost:${port}`;
42-
4342
await metaTest.should("work after deploying artifact", async () => {
44-
const { serialized, typegate: _gateResponseAdd } = await tgDeploy(tg, {
45-
baseUrl: gate,
46-
auth,
47-
artifactsConfig: {
48-
prismaMigration: {
49-
globalAction: {
50-
create: true,
51-
reset: false,
52-
},
53-
migrationDir: "prisma-migrations",
54-
},
55-
dir: cwdDir,
56-
},
57-
typegraphPath: path.join(cwdDir, "wasmedge.ts"),
58-
secrets: {},
59-
});
60-
61-
const engine = await metaTest.engineFromDeployed(serialized);
43+
const engine = await metaTest.engineFromTgDeploy(
44+
"runtimes/wasmedge/wasmedge.ts",
45+
cwd,
46+
tg, // tg should be provided for TS SDK typegraph
47+
);
6248

6349
await gql`
6450
query {
@@ -69,6 +55,5 @@ Meta.test({
6955
test_wasi_ts: 13,
7056
})
7157
.on(engine);
72-
await engine.terminate();
7358
});
7459
});

typegate/tests/utils/test.ts

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Elastic-2.0
33

44
import { SystemTypegraph } from "../../src/system_typegraphs.ts";
5-
import { dirname, join } from "std/path/mod.ts";
5+
import { basename, dirname, extname, join } from "std/path/mod.ts";
66
import { newTempDir, testDir } from "./dir.ts";
77
import { shell, ShellOptions } from "./shell.ts";
88
import { assertSnapshot } from "std/testing/snapshot.ts";
@@ -12,6 +12,7 @@ import { Typegate } from "../../src/typegate/mod.ts";
1212
import { createMetaCli } from "./meta.ts";
1313
import { TypeGraph } from "../../src/typegraph/mod.ts";
1414
import { SyncConfig } from "../../src/sync/config.ts";
15+
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";
1516

1617
type AssertSnapshotParams = typeof assertSnapshot extends (
1718
ctx: Deno.TestContext,
@@ -198,12 +199,84 @@ export class MetaTest {
198199
return engine;
199200
}
200201

201-
async serializeTypegraphFromShell(
202+
async engineFromTgDeploy(path: string, cwd: string, tg?: any) {
203+
const extension = extname(path);
204+
let sdkLang: SDKLangugage;
205+
switch (extension) {
206+
case ".py":
207+
sdkLang = SDKLangugage.Python;
208+
break;
209+
case ".ts":
210+
sdkLang = SDKLangugage.TypeScript;
211+
break;
212+
case ".mjs":
213+
sdkLang = SDKLangugage.TypeScript;
214+
break;
215+
default:
216+
throw new Error("Unsupported file type");
217+
}
218+
219+
const serialized = sdkLang === SDKLangugage.Python
220+
? await this.#serializeTypegraphFromShell(path, sdkLang, cwd)
221+
: await this.#serializeTypegraphFromTgDeployTs(path, cwd, tg);
222+
223+
return await this.engineFromDeployed(serialized);
224+
}
225+
226+
async #serializeTypegraphFromTgDeployTs(path: string, cwd: string, tg?: any) {
227+
if (!this.port) {
228+
throw new Error(
229+
"Error: port option in MetaTest config should be set to 'true'",
230+
);
231+
}
232+
233+
if (!tg) {
234+
throw new Error(
235+
"Error: Typegraph ouput needed for serialzing TS SDK typegraph",
236+
);
237+
}
238+
239+
const auth = new BasicAuth("admin", "password");
240+
const gate = `http://localhost:${this.port}`;
241+
const { serialized, typegate: _gateResponseAdd } = await tgDeploy(tg, {
242+
baseUrl: gate,
243+
auth,
244+
artifactsConfig: {
245+
prismaMigration: {
246+
globalAction: {
247+
create: true,
248+
reset: false,
249+
},
250+
migrationDir: "prisma-migrations",
251+
},
252+
dir: cwd,
253+
},
254+
typegraphPath: join(cwd, basename(path)),
255+
secrets: {},
256+
});
257+
258+
return serialized;
259+
}
260+
261+
async #serializeTypegraphFromShell(
202262
path: string,
203263
lang: SDKLangugage,
264+
cwd: string,
204265
): Promise<string> {
205266
// run self deployed typegraph
206-
const { stderr, stdout } = await this.shell([lang.toString(), path]);
267+
268+
if (!this.port) {
269+
throw new Error(
270+
"Error: port option in MetaTest config should be set to 'true'",
271+
);
272+
}
273+
274+
const { stderr, stdout } = await this.shell([
275+
lang.toString(),
276+
path,
277+
cwd,
278+
this.port.toString(),
279+
]);
207280

208281
if (stderr.length > 0) {
209282
throw new Error(`${stderr}`);

0 commit comments

Comments
 (0)