Files
openclaw/src/shared/global-singleton.test.ts
Vincent Koc 4ca84acf24 fix(runtime): duplicate messages, share singleton state across bundled chunks (#43683)
* Tests: add fresh module import helper

* Process: share command queue runtime state

* Agents: share embedded run runtime state

* Reply: share followup queue runtime state

* Reply: share followup drain callback state

* Reply: share queued message dedupe state

* Reply: share inbound dedupe state

* Tests: cover shared command queue runtime state

* Tests: cover shared embedded run runtime state

* Tests: cover shared followup queue runtime state

* Tests: cover shared inbound dedupe state

* Tests: cover shared Slack thread participation state

* Slack: share sent thread participation state

* Tests: document fresh import helper

* Telegram: share draft stream runtime state

* Tests: cover shared Telegram draft stream state

* Telegram: share sent message cache state

* Tests: cover shared Telegram sent message cache

* Telegram: share thread binding runtime state

* Tests: cover shared Telegram thread binding state

* Tests: avoid duplicate shared queue reset

* refactor(runtime): centralize global singleton access

* refactor(runtime): preserve undefined global singleton values

* test(runtime): cover undefined global singleton values

---------

Co-authored-by: Nimrod Gutman <nimrod.gutman@gmail.com>
2026-03-12 14:59:27 -04:00

40 lines
1.3 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { resolveGlobalMap, resolveGlobalSingleton } from "./global-singleton.js";
const TEST_KEY = Symbol("global-singleton:test");
const TEST_MAP_KEY = Symbol("global-singleton:test-map");
afterEach(() => {
delete (globalThis as Record<PropertyKey, unknown>)[TEST_KEY];
delete (globalThis as Record<PropertyKey, unknown>)[TEST_MAP_KEY];
});
describe("resolveGlobalSingleton", () => {
it("reuses an initialized singleton", () => {
const create = vi.fn(() => ({ value: 1 }));
const first = resolveGlobalSingleton(TEST_KEY, create);
const second = resolveGlobalSingleton(TEST_KEY, create);
expect(first).toBe(second);
expect(create).toHaveBeenCalledTimes(1);
});
it("does not re-run the factory when undefined was already stored", () => {
const create = vi.fn(() => undefined);
expect(resolveGlobalSingleton(TEST_KEY, create)).toBeUndefined();
expect(resolveGlobalSingleton(TEST_KEY, create)).toBeUndefined();
expect(create).toHaveBeenCalledTimes(1);
});
});
describe("resolveGlobalMap", () => {
it("reuses the same map instance", () => {
const first = resolveGlobalMap<string, number>(TEST_MAP_KEY);
const second = resolveGlobalMap<string, number>(TEST_MAP_KEY);
expect(first).toBe(second);
});
});