import assert from "node:assert/strict"; import test from "node:test"; import { ImageClient } from "../src/client"; import { ProtocolError } from "../src/protocol"; import { parseValue } from "../src/values"; import { startImage } from "./server"; test( "extension controls spawn explicit behaviors, send lossless values, and preserve lifecycle state", { timeout: 10000 }, async (t) => { const image = await startImage(); let client: ImageClient | undefined; t.after(async () => { client?.close(); await image.stop(); }); client = await ImageClient.connect(image.address, (error) => assert.equal(error, undefined)); assert.ok((await client.behaviors()).some((behavior) => behavior.name === "counter")); await assert.rejects(client.spawn("missing", parseValue("0")), ProtocolError); const ref = await client.spawn("counter", parseValue("5"), 2); assert.match(ref, /^[A-Za-z0-9_-]{21}$/); await client.send(ref, parseValue('{"op":"add","value":7}')); await client.lifecycle("pause_actor", ref); await client.tick(100); let snapshot = await client.inspectActor(ref); assert.equal(snapshot.status.status, "paused"); assert.deepEqual(snapshot.state, parseValue("5")); assert.equal(snapshot.queued.length, 1); await client.lifecycle("resume_actor", ref); await client.tick(100); snapshot = await client.inspectActor(ref); assert.deepEqual(snapshot.state, parseValue("12")); assert.deepEqual(snapshot.queued, []); await client.lifecycle("stop_actor", ref); assert.equal((await client.inspectActor(ref)).status.status, "stopped"); await client.lifecycle("restart_actor", ref); snapshot = await client.inspectActor(ref); assert.equal(snapshot.incarnation, 2n); assert.deepEqual(snapshot.state, parseValue("12")); const payload = parseValue(`{"nested":[9223372036854775807,{"$actor":"${ref}"}]}`); await client.send(ref, payload); assert.deepEqual((await client.inspectActor(ref)).queued, [payload]); // Whole-request size includes tagged values and the envelope. await assert.rejects( client.send(ref, { type: "string", data: "x".repeat(1024 * 1024) }), /1 MiB/, ); assert.equal((await client.listActors()).length, 1); }, );