Add unit tests for channel utilities and configure testing environment

- Created a new test file `channels.test.ts` to cover utilities related to channel configurations and targets.
- Implemented tests for normalizing and grouping selected channels by type, as well as building channel targets from account data and cron history.
- Mocked necessary dependencies to isolate tests and ensure accurate results.
- Updated `vite.config.ts` to set up the testing environment with jsdom and enable global variables for tests.
This commit is contained in:
duanshuwen
2026-04-18 16:12:49 +08:00
parent ee72cf7261
commit ef46c73c3e
26 changed files with 4056 additions and 186 deletions

View File

@@ -4,6 +4,7 @@ import type { ProviderAccount } from '@runtime/lib/providers';
import { DEFAULT_AGENT_ID, DEFAULT_MAIN_SESSION_SUFFIX, type AgentSummary, type AgentsSnapshot } from '@runtime/lib/agents';
import { buildMainSessionKey, normalizeAgentId } from '@runtime/lib/models';
import { getUserDataDir } from './paths';
import { listStoredChannelTypes } from './channel-config';
interface StoredAgentEntry {
id: string;
@@ -237,6 +238,7 @@ function buildSnapshotFromStore(
];
const configuredChannelTypes = Array.from(new Set([
...listStoredChannelTypes(),
...Object.keys(channelOwners),
...Object.keys(channelAccountOwners).map((key) => key.split(':')[0]).filter(Boolean),
]));
@@ -498,3 +500,28 @@ export function clearChannelBinding(
writeStore(store);
return buildSnapshotFromStore(store, accounts, defaultAccountId);
}
export function clearAllChannelBindings(
channelType: string,
accounts: ProviderAccount[],
defaultAccountId: string | null,
): AgentsSnapshot {
const normalizedChannelType = String(channelType ?? '').trim();
if (!normalizedChannelType) {
throw new Error('channelType is required');
}
const store = readStore();
const nextChannelOwners = { ...(store.channelOwners ?? {}) };
delete nextChannelOwners[normalizedChannelType];
store.channelOwners = nextChannelOwners;
const nextChannelAccountOwners = Object.fromEntries(
Object.entries(store.channelAccountOwners ?? {}).filter(([key]) => !key.startsWith(`${normalizedChannelType}:`)),
);
store.channelAccountOwners = nextChannelAccountOwners;
syncAgentChannelMembership(store, normalizedChannelType);
writeStore(store);
return buildSnapshotFromStore(store, accounts, defaultAccountId);
}