MCPcopy Create free account
hub / github.com/cloudflare/computer / fakeRuntime

Function fakeRuntime

packages/computer/src/client.test.ts:25–76  ·  view source on GitHub ↗
(promisedProperties = false)

Source from the content-addressed store, hash-verified

23// Workspace.runtime (local) and the runtime stub (remote) — the client
24// only needs `exec(command, options?)`.
25function fakeRuntime(promisedProperties = false) {
26 const calls: ExecCall[] = [];
27 let disposedHandles = 0;
28 // A minimal handle satisfying both the local identity rehydrate
29 // (which passes it through) and the remote rebuild (which calls
30 // stream()/result()/kill()). stream() emits one JSONL exit frame so
31 // the rebuilt handle can be iterated.
32 const makeHandle = (id = "x", backend = "test") => ({
33 id: promisedProperties ? Promise.resolve(id) : id,
34 backend: promisedProperties ? Promise.resolve(backend) : backend,
35 result: () => Promise.resolve({ exitCode: 0, stdout: "", stderr: "" }),
36 stream: () => {
37 const stream = new ReadableStream<Uint8Array>({
38 start(c) {
39 c.enqueue(
40 new TextEncoder().encode(`${JSON.stringify({ id, seq: 0, name: "exit", value: 0 })}\n`),
41 );
42 c.close();
43 },
44 });
45 return promisedProperties ? Promise.resolve(stream) : stream;
46 },
47 kill: () => Promise.resolve(),
48 [Symbol.dispose]() {
49 disposedHandles += 1;
50 },
51 });
52 return {
53 calls,
54 disposedHandles: () => disposedHandles,
55 runtime: {
56 exec(command: string, options?: Record<string, unknown>) {
57 calls.push({ command, options });
58 return Promise.resolve(
59 makeHandle(options?.id as string | undefined, options?.backend as string | undefined),
60 );
61 },
62 getExec(id: string, options?: Record<string, unknown>) {
63 calls.push({ command: `get:${id}`, options });
64 return Promise.resolve(makeHandle(id, options?.backend as string | undefined));
65 },
66 killExec(id: string, options?: Record<string, unknown>) {
67 calls.push({ command: `kill:${id}`, options });
68 return Promise.resolve();
69 },
70 disposeExec(id: string, options?: Record<string, unknown>) {
71 calls.push({ command: `dispose:${id}`, options });
72 return Promise.resolve();
73 },
74 },
75 };
76}
77
78// Remote path fake: a stub whose getters mirror WorkspaceStub and
79// whose __getWorkspaceStub resolves to it.

Callers 3

fakeRemoteFunction · 0.85
fakeLocalFunction · 0.85
client.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected