import { describe, expect, test } from "vitest";
class StubActorEnvironment {
    terminatedActors = new Set();
    async open(_opts) {
        return {};
    }
    async create(_opts) { }
    async exists() {
        return false;
    }
    async export() {
        return new ArrayBuffer(0);
    }
    async close() {
        this.terminatedActors.clear();
    }
    async call(name, args) {
        if (name === "lix_exec_sync") {
            return { rows: [] };
        }
        return undefined;
    }
    spawnActor = async (opts) => {
        const listeners = new Set();
        const buffer = [];
        let terminated = false;
        const emit = (message) => {
            if (terminated)
                return;
            buffer.push(message);
            for (const listener of listeners)
                listener(message);
        };
        if (Object.prototype.hasOwnProperty.call(opts, "initialMessage")) {
            emit({ kind: "initial", data: opts.initialMessage });
        }
        const handle = {
            post: (message) => emit({ kind: "post", data: message }),
            subscribe: (listener) => {
                listeners.add(listener);
                for (const message of buffer)
                    listener(message);
                return () => listeners.delete(listener);
            },
            terminate: async () => {
                terminated = true;
                listeners.clear();
            },
        };
        this.terminatedActors.add(handle);
        return handle;
    };
}
describe("LixEnvironment spawnActor", () => {
    test("delivers initial and posted messages", async () => {
        const env = new StubActorEnvironment();
        const actor = await env.spawnActor({
            entryModule: "stub://actor",
            initialMessage: { type: "boot" },
        });
        const events = [];
        const unsubscribe = actor.subscribe((event) => events.push(event));
        actor.post({ type: "ping" });
        await new Promise((resolve) => setTimeout(resolve, 0));
        expect(events).toEqual(expect.arrayContaining([
            { kind: "initial", data: { type: "boot" } },
            { kind: "post", data: { type: "ping" } },
        ]));
        unsubscribe();
        actor.post({ type: "ignored" });
        await new Promise((resolve) => setTimeout(resolve, 0));
        expect(events).toHaveLength(2);
        await actor.terminate();
    });
});
//# sourceMappingURL=api.test.js.map