|
1 | 1 | import { Pept2lca } from '../../../lib/commands/unipept/pept2lca.js'; |
| 2 | +import { vi, describe, test, expect, afterEach } from 'vitest'; |
2 | 3 |
|
3 | | -test('test command setup', () => { |
4 | | - const command = new Pept2lca(); |
5 | | - expect(command.name).toBe("pept2lca"); |
6 | | - expect(command.user_agent).toMatch(/^unipept-cli/); |
7 | | - expect(command.command.name()).toBe("pept2lca"); |
8 | | -}); |
| 4 | +describe('UnipeptSubcommand', () => { |
| 5 | + afterEach(() => { |
| 6 | + vi.restoreAllMocks(); |
| 7 | + }); |
9 | 8 |
|
10 | | -test('test correct host', () => { |
11 | | - const command = new Pept2lca(); |
| 9 | + test('test command setup', () => { |
| 10 | + const command = new Pept2lca(); |
| 11 | + expect(command.name).toBe("pept2lca"); |
| 12 | + expect(command.user_agent).toMatch(/^unipept-cli/); |
| 13 | + expect(command.command.name()).toBe("pept2lca"); |
| 14 | + }); |
12 | 15 |
|
13 | | - expect(command.host).toBe("https://api.unipept.ugent.be"); |
14 | | - expect(command["getHost"]()).toBe("https://api.unipept.ugent.be"); |
| 16 | + test('test correct host', () => { |
| 17 | + const command = new Pept2lca(); |
15 | 18 |
|
16 | | - command.options.host = "https://optionshost"; |
17 | | - expect(command["getHost"]()).toBe("https://optionshost"); |
| 19 | + expect(command.host).toBe("https://api.unipept.ugent.be"); |
| 20 | + expect(command["getHost"]()).toBe("https://api.unipept.ugent.be"); |
18 | 21 |
|
19 | | - command.options.host = "http://optionshost"; |
20 | | - expect(command["getHost"]()).toBe("http://optionshost"); |
| 22 | + command.options.host = "https://optionshost"; |
| 23 | + expect(command["getHost"]()).toBe("https://optionshost"); |
21 | 24 |
|
22 | | - command.options.host = "optionshost"; |
23 | | - expect(command["getHost"]()).toBe("http://optionshost"); |
24 | | -}); |
| 25 | + command.options.host = "http://optionshost"; |
| 26 | + expect(command["getHost"]()).toBe("http://optionshost"); |
25 | 27 |
|
26 | | -test('test correct inputIterator', async () => { |
27 | | - const command = new Pept2lca(); |
| 28 | + command.options.host = "optionshost"; |
| 29 | + expect(command["getHost"]()).toBe("http://optionshost"); |
| 30 | + }); |
28 | 31 |
|
29 | | - // should be stdin |
30 | | - let input = command["getInputIterator"]([]) as AsyncIterableIterator<string>; |
31 | | - expect(typeof input[Symbol.asyncIterator]).toBe("function"); |
32 | | - command['streamInterface']?.close(); |
| 32 | + test('test correct inputIterator', async () => { |
| 33 | + const command = new Pept2lca(); |
33 | 34 |
|
34 | | - // should be a (non-existant) file and error |
35 | | - input = command["getInputIterator"]([], "filename") as AsyncIterableIterator<string>; |
36 | | - expect(typeof input[Symbol.asyncIterator]).toBe("function"); |
37 | | - await expect(async () => { await input.next() }).rejects.toThrow(/no such file/); |
| 35 | + // should be stdin |
| 36 | + let input = command["getInputIterator"]([]) as AsyncIterableIterator<string>; |
| 37 | + expect(typeof input[Symbol.asyncIterator]).toBe("function"); |
| 38 | + command['streamInterface']?.close(); |
38 | 39 |
|
39 | | - // should be array |
40 | | - const inputArray = command["getInputIterator"](["A", "B"]) as IterableIterator<string>; |
41 | | - expect(typeof inputArray[Symbol.iterator]).toBe("function"); |
42 | | -}); |
| 40 | + // should be a (non-existant) file and error |
| 41 | + input = command["getInputIterator"]([], "filename") as AsyncIterableIterator<string>; |
| 42 | + expect(typeof input[Symbol.asyncIterator]).toBe("function"); |
| 43 | + await expect(async () => { await input.next() }).rejects.toThrow(/no such file/); |
43 | 44 |
|
44 | | -test('test selected fields parsing', () => { |
45 | | - const command = new Pept2lca(); |
| 45 | + // should be array |
| 46 | + const inputArray = command["getInputIterator"](["A", "B"]) as IterableIterator<string>; |
| 47 | + expect(typeof inputArray[Symbol.iterator]).toBe("function"); |
| 48 | + }); |
46 | 49 |
|
47 | | - command.options.select = ["a,b,c"]; |
48 | | - expect(command["getSelectedFields"]()).toStrictEqual([/^a$/, /^b$/, /^c$/]); |
49 | | -}); |
| 50 | + test('test selected fields parsing', () => { |
| 51 | + const command = new Pept2lca(); |
| 52 | + |
| 53 | + command.options.select = ["a,b,c"]; |
| 54 | + expect(command["getSelectedFields"]()).toStrictEqual([/^a$/, /^b$/, /^c$/]); |
| 55 | + }); |
| 56 | + |
| 57 | + test('test selected fields with wildcards', () => { |
| 58 | + const command = new Pept2lca(); |
| 59 | + |
| 60 | + command.options.select = ["taxon*,name"]; |
| 61 | + expect(command["getSelectedFields"]()).toStrictEqual([/^taxon.*$/, /^name$/]); |
| 62 | + }); |
| 63 | + |
| 64 | + test('test inputIterator prints warning when reading from TTY stdin', async () => { |
| 65 | + const command = new Pept2lca(); |
| 66 | + |
| 67 | + // Mock process.stdin.isTTY |
| 68 | + const originalIsTTY = process.stdin.isTTY; |
| 69 | + Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true }); |
| 70 | + |
| 71 | + // Mock process.stderr.write |
| 72 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 73 | + |
| 74 | + const input = command["getInputIterator"]([]) as AsyncIterableIterator<string>; |
| 75 | + |
| 76 | + expect(stderrSpy).toHaveBeenCalledWith(expect.stringContaining("Reading from standard input...")); |
| 77 | + |
| 78 | + command['streamInterface']?.close(); |
| 79 | + |
| 80 | + // Restore |
| 81 | + Object.defineProperty(process.stdin, 'isTTY', { value: originalIsTTY }); |
| 82 | + }); |
| 83 | + |
| 84 | + test('test inputIterator does NOT print warning when reading from piped stdin (not TTY)', async () => { |
| 85 | + const command = new Pept2lca(); |
| 86 | + |
| 87 | + // Mock process.stdin.isTTY |
| 88 | + const originalIsTTY = process.stdin.isTTY; |
| 89 | + Object.defineProperty(process.stdin, 'isTTY', { value: false, configurable: true }); |
| 90 | + |
| 91 | + // Mock process.stderr.write |
| 92 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 93 | + |
| 94 | + const input = command["getInputIterator"]([]) as AsyncIterableIterator<string>; |
| 95 | + |
| 96 | + expect(stderrSpy).not.toHaveBeenCalled(); |
50 | 97 |
|
51 | | -test('test selected fields with wildcards', () => { |
52 | | - const command = new Pept2lca(); |
| 98 | + command['streamInterface']?.close(); |
53 | 99 |
|
54 | | - command.options.select = ["taxon*,name"]; |
55 | | - expect(command["getSelectedFields"]()).toStrictEqual([/^taxon.*$/, /^name$/]); |
| 100 | + // Restore |
| 101 | + Object.defineProperty(process.stdin, 'isTTY', { value: originalIsTTY }); |
| 102 | + }); |
56 | 103 | }); |
0 commit comments