/ src / capabilityRouting.test.ts
capabilityRouting.test.ts
 1  import { describe, expect, it } from 'vitest';
 2  import { Strategy, type CliCommand } from './registry.js';
 3  import { shouldUseBrowserSession } from './capabilityRouting.js';
 4  
 5  function makeCmd(partial: Partial<CliCommand>): CliCommand {
 6    return {
 7      site: 'test',
 8      name: 'command',
 9      description: '',
10      args: [],
11      ...partial,
12    };
13  }
14  
15  describe('shouldUseBrowserSession', () => {
16    it('skips browser session for public fetch-only pipelines', () => {
17      expect(shouldUseBrowserSession(makeCmd({
18        browser: true,
19        strategy: Strategy.PUBLIC,
20        pipeline: [{ fetch: 'https://example.com/api' }, { select: 'items' }],
21      }))).toBe(false);
22    });
23  
24    it('keeps browser session for public pipelines with browser-only steps', () => {
25      expect(shouldUseBrowserSession(makeCmd({
26        browser: true,
27        strategy: Strategy.PUBLIC,
28        pipeline: [{ navigate: 'https://example.com' }, { evaluate: '() => []' }],
29      }))).toBe(true);
30    });
31  
32    it('keeps browser session for non-public strategies (via normalized navigateBefore)', () => {
33      // After normalizeCommand, COOKIE strategy without domain sets navigateBefore: true
34      // (signals "needs authenticated browser context" without a specific pre-nav URL).
35      expect(shouldUseBrowserSession(makeCmd({
36        browser: true,
37        strategy: Strategy.COOKIE,
38        navigateBefore: true,
39        pipeline: [{ fetch: 'https://example.com/api' }],
40      }))).toBe(true);
41    });
42  
43    it('keeps browser session for function adapters', () => {
44      expect(shouldUseBrowserSession(makeCmd({
45        browser: true,
46        strategy: Strategy.PUBLIC,
47        func: async () => [],
48      }))).toBe(true);
49    });
50  });