Skip to content

Commit 6a61d55

Browse files
committed
refactor: share extension deferred and runtime helpers
1 parent 1ac4bac commit 6a61d55

File tree

8 files changed

+38
-41
lines changed

8 files changed

+38
-41
lines changed

extensions/irc/src/onboarding.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RuntimeEnv, WizardPrompter } from "openclaw/plugin-sdk/irc";
22
import { describe, expect, it, vi } from "vitest";
3+
import { createRuntimeEnv } from "../../test-utils/runtime-env.js";
34
import { ircOnboardingAdapter } from "./onboarding.js";
45
import type { CoreConfig } from "./types.js";
56

@@ -63,13 +64,7 @@ describe("irc onboarding", () => {
6364
}),
6465
});
6566

66-
const runtime: RuntimeEnv = {
67-
log: vi.fn(),
68-
error: vi.fn(),
69-
exit: vi.fn((code: number): never => {
70-
throw new Error(`exit ${code}`);
71-
}),
72-
};
67+
const runtime: RuntimeEnv = createRuntimeEnv();
7368

7469
const result = await ircOnboardingAdapter.configure({
7570
cfg: {} as CoreConfig,

extensions/matrix/src/channel.directory.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/matrix";
22
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { createRuntimeEnv } from "../../test-utils/runtime-env.js";
34
import { matrixPlugin } from "./channel.js";
45
import { setMatrixRuntime } from "./runtime.js";
56
import { createMatrixBotSdkMock } from "./test-mocks.js";
@@ -10,13 +11,7 @@ vi.mock("@vector-im/matrix-bot-sdk", () =>
1011
);
1112

1213
describe("matrix directory", () => {
13-
const runtimeEnv: RuntimeEnv = {
14-
log: vi.fn(),
15-
error: vi.fn(),
16-
exit: vi.fn((code: number): never => {
17-
throw new Error(`exit ${code}`);
18-
}),
19-
};
14+
const runtimeEnv: RuntimeEnv = createRuntimeEnv();
2015

2116
beforeEach(() => {
2217
setMatrixRuntime({

extensions/matrix/src/channel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
PAIRING_APPROVED_MESSAGE,
1616
type ChannelPlugin,
1717
} from "openclaw/plugin-sdk/matrix";
18+
import { buildTrafficStatusSummary } from "../../shared/channel-status-summary.js";
1819
import { matrixMessageActions } from "./actions.js";
1920
import { MatrixConfigSchema } from "./config-schema.js";
2021
import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
@@ -410,8 +411,7 @@ export const matrixPlugin: ChannelPlugin<ResolvedMatrixAccount> = {
410411
lastError: runtime?.lastError ?? null,
411412
probe,
412413
lastProbeAt: runtime?.lastProbeAt ?? null,
413-
lastInboundAt: runtime?.lastInboundAt ?? null,
414-
lastOutboundAt: runtime?.lastOutboundAt ?? null,
414+
...buildTrafficStatusSummary(runtime),
415415
}),
416416
},
417417
gateway: {

extensions/matrix/src/matrix/send-queue.test.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createDeferred } from "../../../shared/deferred.js";
23
import { DEFAULT_SEND_GAP_MS, enqueueSend } from "./send-queue.js";
34

4-
function deferred<T>() {
5-
let resolve!: (value: T | PromiseLike<T>) => void;
6-
let reject!: (reason?: unknown) => void;
7-
const promise = new Promise<T>((res, rej) => {
8-
resolve = res;
9-
reject = rej;
10-
});
11-
return { promise, resolve, reject };
12-
}
13-
145
describe("enqueueSend", () => {
156
beforeEach(() => {
167
vi.useFakeTimers();
@@ -21,7 +12,7 @@ describe("enqueueSend", () => {
2112
});
2213

2314
it("serializes sends per room", async () => {
24-
const gate = deferred<void>();
15+
const gate = createDeferred<void>();
2516
const events: string[] = [];
2617

2718
const first = enqueueSend("!room:example.org", async () => {
@@ -91,7 +82,7 @@ describe("enqueueSend", () => {
9182
});
9283

9384
it("continues queued work when the head task fails", async () => {
94-
const gate = deferred<void>();
85+
const gate = createDeferred<void>();
9586
const events: string[] = [];
9687

9788
const first = enqueueSend("!room:example.org", async () => {

extensions/nostr/src/channel.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
mapAllowFromEntries,
88
type ChannelPlugin,
99
} from "openclaw/plugin-sdk/nostr";
10-
import { buildPassiveChannelStatusSummary } from "../../shared/channel-status-summary.js";
10+
import {
11+
buildPassiveChannelStatusSummary,
12+
buildTrafficStatusSummary,
13+
} from "../../shared/channel-status-summary.js";
1114
import type { NostrProfile } from "./config-schema.js";
1215
import { NostrConfigSchema } from "./config-schema.js";
1316
import type { MetricEvent, MetricsSnapshot } from "./metrics.js";
@@ -176,8 +179,7 @@ export const nostrPlugin: ChannelPlugin<ResolvedNostrAccount> = {
176179
lastStartAt: runtime?.lastStartAt ?? null,
177180
lastStopAt: runtime?.lastStopAt ?? null,
178181
lastError: runtime?.lastError ?? null,
179-
lastInboundAt: runtime?.lastInboundAt ?? null,
180-
lastOutboundAt: runtime?.lastOutboundAt ?? null,
182+
...buildTrafficStatusSummary(runtime),
181183
}),
182184
},
183185

extensions/shared/channel-status-summary.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ type PassiveChannelStatusSnapshot = {
88
lastProbeAt?: number | null;
99
};
1010

11+
type TrafficStatusSnapshot = {
12+
lastInboundAt?: number | null;
13+
lastOutboundAt?: number | null;
14+
};
15+
1116
export function buildPassiveChannelStatusSummary<TExtra extends object>(
1217
snapshot: PassiveChannelStatusSnapshot,
1318
extra?: TExtra,
@@ -32,3 +37,12 @@ export function buildPassiveProbedChannelStatusSummary<TExtra extends object>(
3237
lastProbeAt: snapshot.lastProbeAt ?? null,
3338
};
3439
}
40+
41+
export function buildTrafficStatusSummary<TSnapshot extends TrafficStatusSnapshot>(
42+
snapshot?: TSnapshot | null,
43+
) {
44+
return {
45+
lastInboundAt: snapshot?.lastInboundAt ?? null,
46+
lastOutboundAt: snapshot?.lastOutboundAt ?? null,
47+
};
48+
}

extensions/shared/deferred.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function createDeferred<T>() {
2+
let resolve!: (value: T | PromiseLike<T>) => void;
3+
let reject!: (reason?: unknown) => void;
4+
const promise = new Promise<T>((res, rej) => {
5+
resolve = res;
6+
reject = rej;
7+
});
8+
return { promise, resolve, reject };
9+
}

extensions/zalouser/src/monitor.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
summarizeMapping,
3232
warnMissingProviderGroupPolicyFallbackOnce,
3333
} from "openclaw/plugin-sdk/zalouser";
34+
import { createDeferred } from "../../shared/deferred.js";
3435
import {
3536
buildZalouserGroupCandidates,
3637
findZalouserGroupEntry,
@@ -129,16 +130,6 @@ function resolveInboundQueueKey(message: ZaloInboundMessage): string {
129130
return `direct:${senderId || threadId}`;
130131
}
131132

132-
function createDeferred<T>() {
133-
let resolve!: (value: T | PromiseLike<T>) => void;
134-
let reject!: (reason?: unknown) => void;
135-
const promise = new Promise<T>((res, rej) => {
136-
resolve = res;
137-
reject = rej;
138-
});
139-
return { promise, resolve, reject };
140-
}
141-
142133
function resolveZalouserDmSessionScope(config: OpenClawConfig) {
143134
const configured = config.session?.dmScope;
144135
return configured === "main" || !configured ? "per-channel-peer" : configured;

0 commit comments

Comments
 (0)