summaryrefslogtreecommitdiff
path: root/packages/core/src/services/ideContext.test.ts
blob: 0e6ff04563cce5362d5bd01995faba1fa90f34f8 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { createIdeContextStore } from './ideContext.js';

describe('ideContext - Active File', () => {
  let ideContext: ReturnType<typeof createIdeContextStore>;

  beforeEach(() => {
    // Create a fresh, isolated instance for each test
    ideContext = createIdeContextStore();
  });

  it('should return undefined initially for active file context', () => {
    expect(ideContext.getActiveFileContext()).toBeUndefined();
  });

  it('should set and retrieve the active file context', () => {
    const testFile = {
      filePath: '/path/to/test/file.ts',
      cursor: { line: 5, character: 10 },
    };

    ideContext.setActiveFileContext(testFile);

    const activeFile = ideContext.getActiveFileContext();
    expect(activeFile).toEqual(testFile);
  });

  it('should update the active file context when called multiple times', () => {
    const firstFile = {
      filePath: '/path/to/first.js',
      cursor: { line: 1, character: 1 },
    };
    ideContext.setActiveFileContext(firstFile);

    const secondFile = {
      filePath: '/path/to/second.py',
      cursor: { line: 20, character: 30 },
    };
    ideContext.setActiveFileContext(secondFile);

    const activeFile = ideContext.getActiveFileContext();
    expect(activeFile).toEqual(secondFile);
  });

  it('should handle empty string for file path', () => {
    const testFile = {
      filePath: '',
      cursor: { line: 0, character: 0 },
    };
    ideContext.setActiveFileContext(testFile);
    expect(ideContext.getActiveFileContext()).toEqual(testFile);
  });

  it('should notify subscribers when active file context changes', () => {
    const subscriber1 = vi.fn();
    const subscriber2 = vi.fn();

    ideContext.subscribeToActiveFile(subscriber1);
    ideContext.subscribeToActiveFile(subscriber2);

    const testFile = {
      filePath: '/path/to/subscribed.ts',
      cursor: { line: 15, character: 25 },
    };
    ideContext.setActiveFileContext(testFile);

    expect(subscriber1).toHaveBeenCalledTimes(1);
    expect(subscriber1).toHaveBeenCalledWith(testFile);
    expect(subscriber2).toHaveBeenCalledTimes(1);
    expect(subscriber2).toHaveBeenCalledWith(testFile);

    // Test with another update
    const newFile = {
      filePath: '/path/to/new.js',
      cursor: { line: 1, character: 1 },
    };
    ideContext.setActiveFileContext(newFile);

    expect(subscriber1).toHaveBeenCalledTimes(2);
    expect(subscriber1).toHaveBeenCalledWith(newFile);
    expect(subscriber2).toHaveBeenCalledTimes(2);
    expect(subscriber2).toHaveBeenCalledWith(newFile);
  });

  it('should stop notifying a subscriber after unsubscribe', () => {
    const subscriber1 = vi.fn();
    const subscriber2 = vi.fn();

    const unsubscribe1 = ideContext.subscribeToActiveFile(subscriber1);
    ideContext.subscribeToActiveFile(subscriber2);

    ideContext.setActiveFileContext({
      filePath: '/path/to/file1.txt',
      cursor: { line: 1, character: 1 },
    });
    expect(subscriber1).toHaveBeenCalledTimes(1);
    expect(subscriber2).toHaveBeenCalledTimes(1);

    unsubscribe1();

    ideContext.setActiveFileContext({
      filePath: '/path/to/file2.txt',
      cursor: { line: 2, character: 2 },
    });
    expect(subscriber1).toHaveBeenCalledTimes(1); // Should not be called again
    expect(subscriber2).toHaveBeenCalledTimes(2);
  });

  it('should allow the cursor to be optional', () => {
    const testFile = {
      filePath: '/path/to/test/file.ts',
    };

    ideContext.setActiveFileContext(testFile);

    const activeFile = ideContext.getActiveFileContext();
    expect(activeFile).toEqual(testFile);
  });
});