kimi-code/packages/node-sdk/test/session-background-tasks.test.ts
haozhe.yang 79ffb7b347 chore: fix lint issues across packages
- remove unused imports and variables
- add void to floating promises
- drop redundant return-await
- bind unbound method references
- replace == null with strict null/undefined checks
- add explicit sort comparators and Array.from helpers
- rename background_tasks capability to tasks
- migrate event sink to event bus in tests
2026-07-07 23:37:34 +08:00

107 lines
4.1 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest';
import { createKimiHarness, type KimiError } from '#/index';
import { makeTempDir, removeTempDirs } from './session-runtime-helpers';
import { TEST_IDENTITY } from './test-identity';
const tempDirs: string[] = [];
afterEach(async () => {
await removeTempDirs(tempDirs);
});
describe('Session.listBackgroundTasks / getBackgroundTaskOutput', () => {
it('lists an empty task set for a fresh session', async () => {
const homeDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-home-');
const workDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-work-');
const harness = createKimiHarness({ homeDir, identity: TEST_IDENTITY });
try {
const session = await harness.createSession({ id: 'ses_bg_list_empty', workDir });
const tasks = await session.listBackgroundTasks();
expect(tasks).toEqual([]);
const filtered = await session.listBackgroundTasks({ activeOnly: true });
expect(filtered).toEqual([]);
} finally {
await harness.close();
}
});
it('returns empty output for an unknown task id', async () => {
const homeDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-home-');
const workDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-work-');
const harness = createKimiHarness({ homeDir, identity: TEST_IDENTITY });
try {
const session = await harness.createSession({ id: 'ses_bg_unknown', workDir });
// Unknown task ids must not throw — UI fetches output speculatively.
await expect(session.getBackgroundTaskOutput('bash-deadbeef')).resolves.toBe('');
} finally {
await harness.close();
}
});
it('rejects empty task ids with a stable error code', async () => {
const homeDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-home-');
const workDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-work-');
const harness = createKimiHarness({ homeDir, identity: TEST_IDENTITY });
try {
const session = await harness.createSession({ id: 'ses_bg_empty_id', workDir });
await expect(session.getBackgroundTaskOutput('')).rejects.toMatchObject({
name: 'KimiError',
code: 'task.task_id_empty',
} satisfies Partial<KimiError>);
await expect(session.stopBackgroundTask('')).rejects.toMatchObject({
name: 'KimiError',
code: 'task.task_id_empty',
} satisfies Partial<KimiError>);
} finally {
await harness.close();
}
});
it('rejects after the session is closed', async () => {
const homeDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-home-');
const workDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-work-');
const harness = createKimiHarness({ homeDir, identity: TEST_IDENTITY });
try {
const session = await harness.createSession({ id: 'ses_bg_closed', workDir });
await session.close();
await expect(session.listBackgroundTasks()).rejects.toMatchObject({
name: 'KimiError',
code: 'session.closed',
} satisfies Partial<KimiError>);
await expect(session.getBackgroundTaskOutput('bash-aaaaaaaa')).rejects.toMatchObject({
name: 'KimiError',
code: 'session.closed',
} satisfies Partial<KimiError>);
await expect(session.stopBackgroundTask('bash-aaaaaaaa')).rejects.toMatchObject({
name: 'KimiError',
code: 'session.closed',
} satisfies Partial<KimiError>);
} finally {
await harness.close();
}
});
it('stopBackgroundTask is a no-op for an unknown task id', async () => {
const homeDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-home-');
const workDir = await makeTempDir(tempDirs, 'kimi-sdk-bgtask-work-');
const harness = createKimiHarness({ homeDir, identity: TEST_IDENTITY });
try {
const session = await harness.createSession({ id: 'ses_bg_stop_unknown', workDir });
// Unknown task ids must not throw — the core BPM silently no-ops.
await expect(
session.stopBackgroundTask('bash-deadbeef', { reason: 'test' }),
).resolves.toBeUndefined();
} finally {
await harness.close();
}
});
});