-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearch.test.ts
More file actions
76 lines (54 loc) · 2.5 KB
/
search.test.ts
File metadata and controls
76 lines (54 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {ux} from '@oclif/core';
import {expect} from 'chai';
import {afterEach, beforeEach} from 'mocha';
import sinon from 'sinon';
import DocsSearch from '../../../src/commands/docs/search.js';
import {createIsolatedConfigHooks, createTestCommand, runSilent} from '../../helpers/test-setup.js';
describe('docs search', () => {
const hooks = createIsolatedConfigHooks();
beforeEach(hooks.beforeEach);
afterEach(hooks.afterEach);
async function createCommand(flags: Record<string, unknown>, args: Record<string, unknown>) {
return createTestCommand(DocsSearch, hooks.getConfig(), flags, args);
}
it('errors when query is missing in search mode', async () => {
const command: any = await createCommand({}, {});
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
try {
await command.run();
expect.fail('Should have thrown');
} catch {
// expected
}
expect(errorStub.calledOnce).to.equal(true);
});
it('lists docs in json mode', async () => {
const command: any = await createCommand({list: true, json: true}, {});
const listStub = sinon.stub().returns([{id: 'a', title: 'A', filePath: 'a.md'}]);
command.operations = {...command.operations, listDocs: listStub};
const result = await runSilent(() => command.run());
expect(result.entries).to.have.length(1);
});
it('prints no results message when search returns empty in non-json mode', async () => {
const command: any = await createCommand({limit: 5}, {query: 'x'});
sinon.stub(command, 'jsonEnabled').returns(false);
const searchStub = sinon.stub().returns([]);
command.operations = {...command.operations, searchDocs: searchStub};
const stdoutStub = sinon.stub(ux, 'stdout');
const result = await command.run();
expect(result.results).to.have.length(0);
expect(stdoutStub.calledOnce).to.equal(true);
});
it('returns results in json mode', async () => {
const command: any = await createCommand({json: true, limit: 5}, {query: 'x'});
const searchStub = sinon.stub().returns([{entry: {id: 'a', title: 'A', filePath: 'a.md'}, score: 0.1}]);
command.operations = {...command.operations, searchDocs: searchStub};
const result = await runSilent(() => command.run());
expect(result.results).to.have.length(1);
});
});