@dxos/functions-testing 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/index.mjs +0 -0
- package/dist/lib/testing.mjs +93 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/src/testing/util.d.ts +2 -2
- package/dist/types/src/testing/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +23 -22
- package/src/bundle.test.ts +1 -1
- package/src/edge-routine.test.ts +1 -4
- package/src/testing/util.ts +5 -5
- package/dist/lib/node-esm/index.mjs +0 -2
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -115
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
|
File without changes
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from "@dxos/node-std/fs";
|
|
2
|
+
import { sleep } from "@dxos/async";
|
|
3
|
+
import { Client } from "@dxos/client";
|
|
4
|
+
import { Operation, Trigger } from "@dxos/compute";
|
|
5
|
+
import { InvocationTraceEndEvent, InvocationTraceStartEvent } from "@dxos/compute-runtime";
|
|
6
|
+
import { Context } from "@dxos/context";
|
|
7
|
+
import { Feed, Filter, Obj, Query, Scope } from "@dxos/echo";
|
|
8
|
+
import { FunctionsServiceClient } from "@dxos/edge-compute";
|
|
9
|
+
import { bundleFunction } from "@dxos/edge-compute/native";
|
|
10
|
+
import { ErrorCodec } from "@dxos/protocols";
|
|
11
|
+
import { EdgeReplicationSetting } from "@dxos/protocols/proto/dxos/echo/metadata";
|
|
12
|
+
//#region src/testing/util.ts
|
|
13
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/compute/functions-testing/src/testing/util.ts";
|
|
14
|
+
var writeBundle = (path, bundle) => {
|
|
15
|
+
fs.mkdirSync(path, { recursive: true });
|
|
16
|
+
Object.entries(bundle.assets).forEach(([name, content]) => {
|
|
17
|
+
fs.writeFileSync(path + "/" + name, content);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
var setup = async (config) => {
|
|
21
|
+
const client = await new Client({
|
|
22
|
+
config,
|
|
23
|
+
types: [Operation.PersistentOperation, Trigger.Trigger]
|
|
24
|
+
}).initialize();
|
|
25
|
+
await client.halo.createIdentity();
|
|
26
|
+
const space = await client.spaces.create();
|
|
27
|
+
await space.waitUntilReady();
|
|
28
|
+
await space.internal.setEdgeReplicationPreference(EdgeReplicationSetting.ENABLED);
|
|
29
|
+
return {
|
|
30
|
+
client,
|
|
31
|
+
space,
|
|
32
|
+
functionsServiceClient: FunctionsServiceClient.fromClient(client)
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
var sync = async (space) => {
|
|
36
|
+
await space.db.flush();
|
|
37
|
+
await space.internal.syncToEdge({ onProgress: (state) => console.log(state ? `${state.unsyncedDocumentCount} documents syncing...` : "connecting to edge...") });
|
|
38
|
+
};
|
|
39
|
+
var deployFunction = async (space, functionsServiceClient, entryPoint, runtime, ownerUri) => {
|
|
40
|
+
const artifact = await bundleFunction({
|
|
41
|
+
entryPoint,
|
|
42
|
+
verbose: true
|
|
43
|
+
});
|
|
44
|
+
const func = await functionsServiceClient.deploy(Context.default(void 0, {
|
|
45
|
+
"~LogMeta": "~LogMeta",
|
|
46
|
+
F: __dxlog_file,
|
|
47
|
+
L: 62
|
|
48
|
+
}), {
|
|
49
|
+
version: "0.0.1",
|
|
50
|
+
ownerUri,
|
|
51
|
+
entryPoint: artifact.entryPoint,
|
|
52
|
+
assets: artifact.assets,
|
|
53
|
+
runtime
|
|
54
|
+
});
|
|
55
|
+
space.db.add(func);
|
|
56
|
+
return func;
|
|
57
|
+
};
|
|
58
|
+
var observeInvocations = async (space, maxCount) => {
|
|
59
|
+
let initialCount = null;
|
|
60
|
+
const invocationData = /* @__PURE__ */ new Map();
|
|
61
|
+
let count = 0;
|
|
62
|
+
while (true) {
|
|
63
|
+
try {
|
|
64
|
+
const traceFeed = space.properties.invocationTraceFeed?.target;
|
|
65
|
+
const traceFeedDXN = traceFeed ? Feed.getFeedUri(traceFeed) : void 0;
|
|
66
|
+
const invocations = traceFeedDXN ? await space.db.query(Query.select(Filter.everything()).from(Scope.feed(traceFeedDXN))).run() : [];
|
|
67
|
+
for (const invocation of invocations) if (Obj.instanceOf(InvocationTraceStartEvent, invocation)) {
|
|
68
|
+
if (invocationData.has(invocation.invocationId)) continue;
|
|
69
|
+
invocationData.set(invocation.invocationId, {
|
|
70
|
+
count: count++,
|
|
71
|
+
begin: invocation
|
|
72
|
+
});
|
|
73
|
+
console.log(`${count.toString().padStart(3, " ")}: BEGIN ${invocation.runtime ?? "(unknown runtime)"} ${JSON.stringify(invocation.input)}`);
|
|
74
|
+
} else if (Obj.instanceOf(InvocationTraceEndEvent, invocation)) {
|
|
75
|
+
const data = invocationData.get(invocation.invocationId);
|
|
76
|
+
if (!data || !!data.end) continue;
|
|
77
|
+
data.end = invocation;
|
|
78
|
+
const outcome = data.end.outcome;
|
|
79
|
+
console.log(`${data.count.toString().padStart(3, " ")}: END outcome=${outcome} duration=${data.end.timestamp - data.begin.timestamp}`);
|
|
80
|
+
if (outcome === "failure" && data.end.error) console.error(ErrorCodec.decode(data.end.error));
|
|
81
|
+
}
|
|
82
|
+
if (initialCount === null) initialCount = invocations.length;
|
|
83
|
+
if (maxCount !== null && invocationData.size >= maxCount + initialCount) break;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
console.error(err);
|
|
86
|
+
}
|
|
87
|
+
await sleep(1e3);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
//#endregion
|
|
91
|
+
export { deployFunction, observeInvocations, setup, sync, writeBundle };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=testing.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.mjs","names":[],"sources":["../../src/testing/util.ts"],"sourcesContent":["//\n// Copyright 2025 DXOS.org\n//\n\nimport fs from 'node:fs';\n\nimport { sleep } from '@dxos/async';\nimport { Client, type Config } from '@dxos/client';\nimport { type Space } from '@dxos/client/echo';\nimport { Operation, Trigger } from '@dxos/compute';\nimport { InvocationTraceEndEvent, InvocationTraceStartEvent } from '@dxos/compute-runtime';\nimport { Context } from '@dxos/context';\nimport { Feed, Filter, Obj, Query, Scope } from '@dxos/echo';\nimport { FunctionsServiceClient } from '@dxos/edge-compute';\nimport { bundleFunction } from '@dxos/edge-compute/native';\nimport type { BundleResult } from '@dxos/edge-compute/native';\nimport { ErrorCodec, type FunctionRuntimeKind } from '@dxos/protocols';\nimport { EdgeReplicationSetting } from '@dxos/protocols/proto/dxos/echo/metadata';\n\nexport const writeBundle = (path: string, bundle: BundleResult) => {\n fs.mkdirSync(path, { recursive: true });\n Object.entries(bundle.assets).forEach(([name, content]) => {\n fs.writeFileSync(path + '/' + name, content);\n });\n};\n\nexport const setup = async (config: Config) => {\n const client = await new Client({\n config,\n types: [Operation.PersistentOperation, Trigger.Trigger],\n }).initialize();\n await client.halo.createIdentity();\n\n const space = await client.spaces.create();\n await space.waitUntilReady();\n await space.internal.setEdgeReplicationPreference(EdgeReplicationSetting.ENABLED);\n\n const functionsServiceClient = FunctionsServiceClient.fromClient(client);\n\n return { client, space, functionsServiceClient };\n};\n\nexport const sync = async (space: Space) => {\n await space.db.flush();\n await space.internal.syncToEdge({\n onProgress: (state) =>\n console.log(state ? `${state.unsyncedDocumentCount} documents syncing...` : 'connecting to edge...'),\n });\n};\n\nexport const deployFunction = async (\n space: Space,\n functionsServiceClient: FunctionsServiceClient,\n entryPoint: string,\n runtime: FunctionRuntimeKind,\n ownerUri: string,\n): Promise<Operation.PersistentOperation> => {\n const artifact = await bundleFunction({\n entryPoint,\n verbose: true,\n });\n const func = await functionsServiceClient.deploy(Context.default(), {\n version: '0.0.1',\n ownerUri,\n entryPoint: artifact.entryPoint,\n assets: artifact.assets,\n runtime,\n });\n space.db.add(func);\n\n return func;\n};\n\nexport const observeInvocations = async (space: Space, maxCount: number | null) => {\n let initialCount = null;\n const invocationData = new Map<\n string,\n {\n count: number;\n begin: InvocationTraceStartEvent;\n end?: InvocationTraceEndEvent;\n }\n >();\n let count = 0;\n while (true) {\n try {\n const traceFeed = space.properties.invocationTraceFeed?.target;\n const traceFeedDXN = traceFeed ? Feed.getFeedUri(traceFeed) : undefined;\n const invocations = traceFeedDXN\n ? await space.db.query(Query.select(Filter.everything()).from(Scope.feed(traceFeedDXN))).run()\n : [];\n\n for (const invocation of invocations) {\n if (Obj.instanceOf(InvocationTraceStartEvent, invocation)) {\n if (invocationData.has(invocation.invocationId)) {\n continue;\n }\n invocationData.set(invocation.invocationId, {\n count: count++,\n begin: invocation,\n });\n console.log(\n `${count.toString().padStart(3, ' ')}: BEGIN ${invocation.runtime ?? '(unknown runtime)'} ${JSON.stringify(invocation.input)}`,\n );\n } else if (Obj.instanceOf(InvocationTraceEndEvent, invocation)) {\n const data = invocationData.get(invocation.invocationId);\n if (!data || !!data.end) {\n continue;\n }\n data.end = invocation;\n\n const outcome = data.end.outcome;\n console.log(\n `${data.count.toString().padStart(3, ' ')}: END outcome=${outcome} duration=${data.end.timestamp - data.begin.timestamp}`,\n );\n if (outcome === 'failure' && data.end.error) {\n console.error(ErrorCodec.decode(data.end.error));\n }\n }\n }\n if (initialCount === null) {\n initialCount = invocations.length;\n }\n\n if (maxCount !== null && invocationData.size >= maxCount + initialCount) {\n break;\n }\n } catch (err) {\n console.error(err);\n }\n await sleep(1_000);\n }\n};\n"],"mappings":";;;;;;;;;;;;;AAmBA,IAAa,eAAe,MAAc,WAAyB;CACjE,GAAG,UAAU,MAAM,EAAE,WAAW,KAAK,CAAC;CACtC,OAAO,QAAQ,OAAO,MAAM,CAAC,CAAC,SAAS,CAAC,MAAM,aAAa;EACzD,GAAG,cAAc,OAAO,MAAM,MAAM,OAAO;CAC7C,CAAC;AACH;AAEA,IAAa,QAAQ,OAAO,WAAmB;CAC7C,MAAM,SAAS,MAAM,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AC;GAEL,KAAK,MAAM,cAAc,aACvB,IAAI,IAAI,WAAW,2BAA2B,UAAU,GAAG;IACzD,IAAI,eAAe,IAAI,WAAW,YAAY,GAC5C;IAEF,eAAe,IAAI,WAAW,cAAc;KAC1C,OAAO;KACP,OAAO;IACT,CAAC;IACD,QAAQ,IACN,GAAG,MAAM,SAAS,CAAC,CAAC,SAAS,GAAG,GAAG,EAAE,UAAU,WAAW,WAAW,oBAAoB,GAAG,KAAK,UAAU,WAAW,KAAK,GAC7H;GACF,OAAO,IAAI,IAAI,WAAW,yBAAyB,UAAU,GAAG;IAC9D,MAAM,OAAO,eAAe,IAAI,WAAW,YAAY;IACvD,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,KAClB;IAEF,KAAK,MAAM;IAEX,MAAM,UAAU,KAAK,IAAI;IACzB,QAAQ,IACN,GAAG,KAAK,MAAM,SAAS,CAAC,CAAC,SAAS,GAAG,GAAG,EAAE,gBAAgB,QAAQ,YAAY,KAAK,IAAI,YAAY,KAAK,MAAM,WAChH;IACA,IAAI,YAAY,aAAa,KAAK,IAAI,OACpC,QAAQ,MAAM,WAAW,OAAO,KAAK,IAAI,KAAK,CAAC;GAEnD;GAEF,IAAI,iBAAiB,MACnB,eAAe,YAAY;GAG7B,IAAI,aAAa,QAAQ,eAAe,QAAQ,WAAW,cACzD;EAEJ,SAAS,KAAK;GACZ,QAAQ,MAAM,GAAG;EACnB;EACA,MAAM,MAAM,GAAK;CACnB;AACF"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Client, type Config } from '@dxos/client';
|
|
2
2
|
import { type Space } from '@dxos/client/echo';
|
|
3
3
|
import { Operation } from '@dxos/compute';
|
|
4
|
-
import { FunctionsServiceClient } from '@dxos/
|
|
5
|
-
import type { BundleResult } from '@dxos/
|
|
4
|
+
import { FunctionsServiceClient } from '@dxos/edge-compute';
|
|
5
|
+
import type { BundleResult } from '@dxos/edge-compute/native';
|
|
6
6
|
import { type FunctionRuntimeKind } from '@dxos/protocols';
|
|
7
7
|
export declare const writeBundle: (path: string, bundle: BundleResult) => void;
|
|
8
8
|
export declare const setup: (config: Config) => Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../../src/testing/util.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAW,MAAM,eAAe,CAAC;AAInD,OAAO,EAAE,sBAAsB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../../src/testing/util.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAW,MAAM,eAAe,CAAC;AAInD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAc,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGvE,eAAO,MAAM,WAAW,SAAU,MAAM,UAAU,YAAY,SAK7D,CAAC;AAEF,eAAO,MAAM,KAAK,WAAkB,MAAM;;;;EAczC,CAAC;AAEF,eAAO,MAAM,IAAI,UAAiB,KAAK,kBAMtC,CAAC;AAEF,eAAO,MAAM,cAAc,UAClB,KAAK,0BACY,sBAAsB,cAClC,MAAM,WACT,mBAAmB,YAClB,MAAM,KACf,OAAO,CAAC,SAAS,CAAC,mBAAmB,CAevC,CAAC;AAEF,eAAO,MAAM,kBAAkB,UAAiB,KAAK,YAAY,MAAM,GAAG,IAAI,kBA2D7E,CAAC"}
|