refactor: extend shared account status snapshot helpers

This commit is contained in:
Peter Steinberger
2026-03-22 20:50:22 +00:00
parent 00b2f10dec
commit 87722d6327
15 changed files with 292 additions and 199 deletions

View File

@@ -88,6 +88,34 @@ describe("buildBaseAccountStatusSnapshot", () => {
lastOutboundAt: null,
});
});
it("merges extra snapshot fields after the shared account shape", () => {
expect(
buildBaseAccountStatusSnapshot(
{
account: { accountId: "default", configured: true },
},
{
connected: true,
mode: "polling",
},
),
).toEqual({
accountId: "default",
name: undefined,
enabled: undefined,
configured: true,
running: false,
lastStartAt: null,
lastStopAt: null,
lastError: null,
probe: undefined,
lastInboundAt: null,
lastOutboundAt: null,
connected: true,
mode: "polling",
});
});
});
describe("buildComputedAccountStatusSnapshot", () => {
@@ -112,6 +140,33 @@ describe("buildComputedAccountStatusSnapshot", () => {
lastOutboundAt: null,
});
});
it("merges computed extras after the shared fields", () => {
expect(
buildComputedAccountStatusSnapshot(
{
accountId: "default",
configured: true,
},
{
connected: true,
},
),
).toEqual({
accountId: "default",
name: undefined,
enabled: undefined,
configured: true,
running: false,
lastStartAt: null,
lastStopAt: null,
lastError: null,
probe: undefined,
lastInboundAt: null,
lastOutboundAt: null,
connected: true,
});
});
});
describe("buildRuntimeAccountStatusSnapshot", () => {
@@ -124,6 +179,17 @@ describe("buildRuntimeAccountStatusSnapshot", () => {
probe: undefined,
});
});
it("merges extra fields into runtime snapshots", () => {
expect(buildRuntimeAccountStatusSnapshot({}, { port: 3978 })).toEqual({
running: false,
lastStartAt: null,
lastStopAt: null,
lastError: null,
probe: undefined,
port: 3978,
});
});
});
describe("buildTokenChannelStatusSummary", () => {

View File

@@ -17,6 +17,8 @@ type RuntimeLifecycleSnapshot = {
lastOutboundAt?: number | null;
};
type StatusSnapshotExtra = Record<string, unknown>;
/** Create the baseline runtime snapshot shape used by channel/account status stores. */
export function createDefaultChannelRuntimeState<T extends Record<string, unknown>>(
accountId: string,
@@ -77,16 +79,19 @@ export function buildProbeChannelStatusSummary<TExtra extends Record<string, unk
}
/** Build the standard per-account status payload from config metadata plus runtime state. */
export function buildBaseAccountStatusSnapshot(params: {
account: {
accountId: string;
name?: string;
enabled?: boolean;
configured?: boolean;
};
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
}) {
export function buildBaseAccountStatusSnapshot<TExtra extends StatusSnapshotExtra>(
params: {
account: {
accountId: string;
name?: string;
enabled?: boolean;
configured?: boolean;
};
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
},
extra?: TExtra,
) {
const { account, runtime, probe } = params;
return {
accountId: account.accountId,
@@ -96,36 +101,46 @@ export function buildBaseAccountStatusSnapshot(params: {
...buildRuntimeAccountStatusSnapshot({ runtime, probe }),
lastInboundAt: runtime?.lastInboundAt ?? null,
lastOutboundAt: runtime?.lastOutboundAt ?? null,
...(extra ?? ({} as TExtra)),
};
}
/** Convenience wrapper when the caller already has flattened account fields instead of an account object. */
export function buildComputedAccountStatusSnapshot(params: {
accountId: string;
name?: string;
enabled?: boolean;
configured?: boolean;
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
}) {
export function buildComputedAccountStatusSnapshot<TExtra extends StatusSnapshotExtra>(
params: {
accountId: string;
name?: string;
enabled?: boolean;
configured?: boolean;
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
},
extra?: TExtra,
) {
const { accountId, name, enabled, configured, runtime, probe } = params;
return buildBaseAccountStatusSnapshot({
account: {
accountId,
name,
enabled,
configured,
return buildBaseAccountStatusSnapshot(
{
account: {
accountId,
name,
enabled,
configured,
},
runtime,
probe,
},
runtime,
probe,
});
extra,
);
}
/** Normalize runtime-only account state into the shared status snapshot fields. */
export function buildRuntimeAccountStatusSnapshot(params: {
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
}) {
export function buildRuntimeAccountStatusSnapshot<TExtra extends StatusSnapshotExtra>(
params: {
runtime?: RuntimeLifecycleSnapshot | null;
probe?: unknown;
},
extra?: TExtra,
) {
const { runtime, probe } = params;
return {
running: runtime?.running ?? false,
@@ -133,6 +148,7 @@ export function buildRuntimeAccountStatusSnapshot(params: {
lastStopAt: runtime?.lastStopAt ?? null,
lastError: runtime?.lastError ?? null,
probe,
...(extra ?? ({} as TExtra)),
};
}