feat(onebot): expose account session uptime
Expose account session uptime in status queries, matching the built-in status reply. Fixes #486
This commit is contained in:
9 files changed
+46
-6
No files matched your search
@@ -112,6 +112,7 @@ function makeRef(overrides: {
|
||||
reactionStore?: Record<string, unknown>;
|
||||
converterCtx?: Record<string, unknown>;
|
||||
extraBridge?: Record<string, unknown>;
|
||||
getUptimeMs?: () => number;
|
||||
} = {}): {
|
||||
ref: OneBotInstanceContext;
|
||||
api: ReturnType<typeof buildApiContext>;
|
||||
@@ -253,6 +254,7 @@ function makeRef(overrides: {
|
||||
} as OneBotConfig,
|
||||
cacheMessageMeta,
|
||||
dispatchEvent,
|
||||
getUptimeMs: overrides.getUptimeMs ?? (() => 0),
|
||||
} as unknown as OneBotInstanceContext;
|
||||
|
||||
return {
|
||||
@@ -266,6 +268,17 @@ function makeRef(overrides: {
|
||||
}
|
||||
|
||||
describe('buildApiContext capabilities', () => {
|
||||
it('reads current uptime from each account session independently', () => {
|
||||
let firstUptime = 1_999;
|
||||
const first = makeRef({ getUptimeMs: () => firstUptime });
|
||||
const second = makeRef({ getUptimeMs: () => 0 });
|
||||
expect(first.api.getUptimeMs()).toBe(1_999);
|
||||
expect(second.api.getUptimeMs()).toBe(0);
|
||||
firstUptime = 3_001;
|
||||
expect(first.api.getUptimeMs()).toBe(3_001);
|
||||
expect(makeRef({ getUptimeMs: () => 0 }).api.getUptimeMs()).toBe(0);
|
||||
});
|
||||
|
||||
it('reports the account as online and able to send image or record', () => {
|
||||
const { api } = makeRef();
|
||||
expect(api.isOnline()).toBe(true);
|
||||
|
||||
@@ -2,9 +2,20 @@ import { describe, expect, it } from 'vitest';
|
||||
import { ApiHandler, type ApiActionContext } from '../src/api-handler';
|
||||
|
||||
describe('get_status transport health (#233, #342)', () => {
|
||||
it.each([0, 999, 1_000, 90_999])('reports whole session seconds for %i ms', async (uptimeMs) => {
|
||||
const ctx = {
|
||||
isOnline: () => true,
|
||||
getUptimeMs: () => uptimeMs,
|
||||
bridge: { receiveHealthy: true },
|
||||
} as unknown as ApiActionContext;
|
||||
const response = await new ApiHandler(ctx).handle('get_status', {});
|
||||
expect(response.data).toEqual({ online: true, good: true, time: Math.floor(uptimeMs / 1000) });
|
||||
});
|
||||
|
||||
it('keeps online true while reporting a stalled receive path as not good', async () => {
|
||||
const ctx = {
|
||||
isOnline: () => true,
|
||||
getUptimeMs: () => 90_999,
|
||||
bridge: { receiveHealthy: false },
|
||||
} as unknown as ApiActionContext;
|
||||
const response = await new ApiHandler(ctx).handle('get_status', {});
|
||||
@@ -12,7 +23,7 @@ describe('get_status transport health (#233, #342)', () => {
|
||||
expect(response).toMatchObject({
|
||||
status: 'ok',
|
||||
retcode: 0,
|
||||
data: { online: true, good: false },
|
||||
data: { online: true, good: false, time: 90 },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user