60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
|
|
import assert from "node:assert/strict";
|
||
|
|
import test from "node:test";
|
||
|
|
import { formatSnapshot } from "../src/protocol";
|
||
|
|
import { canEditActor, parseSnapshotEdit } from "../src/snapshotEdits";
|
||
|
|
import { actor } from "./fixtures";
|
||
|
|
|
||
|
|
test("inspection edits retain exact values and separate protected fields", () => {
|
||
|
|
const snapshot = actor();
|
||
|
|
snapshot.execution = null;
|
||
|
|
const original = formatSnapshot(snapshot, "127.0.0.1:3737", "captured", true);
|
||
|
|
const edited = {
|
||
|
|
...snapshot,
|
||
|
|
actor_ref: "changed",
|
||
|
|
log_level: "warn" as const,
|
||
|
|
reserved: null,
|
||
|
|
queued: [{ type: "integer" as const, data: -9223372036854775808n }],
|
||
|
|
state: { type: "integer" as const, data: 9223372036854775807n },
|
||
|
|
};
|
||
|
|
const source = formatSnapshot(edited, "different", "captured", true);
|
||
|
|
const edit = parseSnapshotEdit(source, original);
|
||
|
|
assert.deepEqual(edit.state, edited.state);
|
||
|
|
assert.deepEqual(edit.messages, { reserved: null, queued: edited.queued });
|
||
|
|
assert.equal(edit.level, "warn");
|
||
|
|
assert.deepEqual(edit.discarded, ["connection", "actor.actor_ref"]);
|
||
|
|
const protectedNumber = parseSnapshotEdit(
|
||
|
|
source.replace('"incarnation": 1', '"incarnation": 1.5'),
|
||
|
|
original,
|
||
|
|
);
|
||
|
|
assert.ok(protectedNumber.discarded.includes("actor.incarnation"));
|
||
|
|
assert.equal(canEditActor(snapshot), true);
|
||
|
|
snapshot.status = { status: "idle" };
|
||
|
|
assert.equal(canEditActor(snapshot), false);
|
||
|
|
snapshot.status = { status: "paused" };
|
||
|
|
snapshot.execution = { vm_steps: 1n, instruction_pointer: 1n, call_depth: 1n };
|
||
|
|
assert.equal(canEditActor(snapshot), false);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("inspection edits validate all writable fields before saving", () => {
|
||
|
|
const original = formatSnapshot(actor(), "endpoint", "captured", true);
|
||
|
|
assert.throws(
|
||
|
|
() => parseSnapshotEdit(original.replace('"info"', '"nope"'), original),
|
||
|
|
/log_level/,
|
||
|
|
);
|
||
|
|
assert.throws(() =>
|
||
|
|
parseSnapshotEdit(original.replace("9223372036854775807", "9223372036854775808"), original),
|
||
|
|
);
|
||
|
|
assert.throws(() => parseSnapshotEdit("{}", original), /actor object/);
|
||
|
|
assert.throws(
|
||
|
|
() =>
|
||
|
|
parseSnapshotEdit(original.replace('"queued": [', '"queued": null, "extra": ['), original),
|
||
|
|
/queued/,
|
||
|
|
);
|
||
|
|
assert.deepEqual(parseSnapshotEdit(original, original), {
|
||
|
|
state: null,
|
||
|
|
messages: null,
|
||
|
|
level: undefined,
|
||
|
|
discarded: [],
|
||
|
|
});
|
||
|
|
});
|