Files
jackwener__opencli/src/commands/daemon.test.ts
jakevin 4a0b8054b2 fix: batch quality improvements — dedupe completion, unify logging, fix docs (#945)
* fix: batch quality improvements — dedupe completion, unify logging, fix docs

1. Extract shared completion code (BUILTIN_COMMANDS + shell scripts) into
   completion-shared.ts, eliminating duplication between completion.ts and
   completion-fast.ts.

2. Replace console.error/warn/log with log.* from logger.ts in:
   - daemon.ts (7 occurrences)
   - runtime.ts (1 occurrence)
   - cli.ts browserAction error handler (3 occurrences)
   - base-page.ts snapshot fallback (1 occurrence)
   - download/index.ts cookie warning (1 occurrence)
   - commands/daemon.ts (2 occurrences)

3. Fix Node version in build-extension.yml: 20 → 22 (matches package.json >=21)

4. Fix error handling consistency: tap.ts now throws CliError instead of bare Error

5. Remove 31 duplicate rows in docs/adapters/index.md (grok, gemini, yuanbao,
   notebooklm, doubao, weread + 25 more entries duplicated without .md suffix)

6. Update skill version: opencli-usage SKILL.md 1.6.9 → 1.7.0, adapter count 79 → 87

* fix: update daemon.test.ts to match logger migration

Tests now spy on process.stderr.write (used by log.*) instead of
console.log/console.error (no longer used by daemonStop).

* fix: address review feedback on PR #945

1. base-page.ts: restore DEBUG_SNAPSHOT env guard — log.debug uses a
   different env var (DEBUG=opencli), so keep the original gate to
   avoid breaking existing users.

2. daemon.ts: remove dead `prefix` variable left over from console.error
   migration.
2026-04-11 01:45:00 +08:00

74 lines
1.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const {
fetchDaemonStatusMock,
requestDaemonShutdownMock,
} = vi.hoisted(() => ({
fetchDaemonStatusMock: vi.fn(),
requestDaemonShutdownMock: vi.fn(),
}));
vi.mock('../browser/daemon-client.js', () => ({
fetchDaemonStatus: fetchDaemonStatusMock,
requestDaemonShutdown: requestDaemonShutdownMock,
}));
import { daemonStop } from './daemon.js';
describe('daemonStop', () => {
let stderrSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
fetchDaemonStatusMock.mockReset();
requestDaemonShutdownMock.mockReset();
});
afterEach(() => {
vi.restoreAllMocks();
});
it('reports "not running" when daemon is unreachable', async () => {
fetchDaemonStatusMock.mockResolvedValue(null);
await daemonStop();
expect(stderrSpy).toHaveBeenCalledWith(expect.stringContaining('not running'));
});
it('sends shutdown and reports success', async () => {
fetchDaemonStatusMock.mockResolvedValue({
ok: true,
pid: 12345,
uptime: 100,
extensionConnected: true,
pending: 0,
memoryMB: 50,
port: 19825,
});
requestDaemonShutdownMock.mockResolvedValue(true);
await daemonStop();
expect(requestDaemonShutdownMock).toHaveBeenCalledTimes(1);
expect(stderrSpy).toHaveBeenCalledWith(expect.stringContaining('Daemon stopped'));
});
it('reports failure when shutdown request fails', async () => {
fetchDaemonStatusMock.mockResolvedValue({
ok: true,
pid: 12345,
uptime: 100,
extensionConnected: true,
pending: 0,
memoryMB: 50,
port: 19825,
});
requestDaemonShutdownMock.mockResolvedValue(false);
await daemonStop();
expect(stderrSpy).toHaveBeenCalledWith(expect.stringContaining('Failed to stop daemon'));
});
});