File size: 4,083 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import fs from 'node:fs';
import path from 'node:path';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { SettingScope } from '../../config/settings.js';
import { getLiteRtBinDir } from './constants.js';

const mockLoadSettings = vi.hoisted(() => vi.fn());

vi.mock('../../config/settings.js', () => ({
  loadSettings: mockLoadSettings,
  SettingScope: {
    User: 'User',
  },
}));

import {
  getBinaryPath,
  isExpectedLiteRtServerCommand,
  isBinaryInstalled,
  readServerProcessInfo,
  resolveGemmaConfig,
} from './platform.js';

describe('gemma platform helpers', () => {
  function createMockSettings(
    userGemmaSettings?: object,
    mergedGemmaSettings?: object,
  ) {
    return {
      merged: {
        experimental: {
          gemmaModelRouter: mergedGemmaSettings,
        },
      },
      forScope: vi.fn((scope: SettingScope) => {
        if (scope !== SettingScope.User) {
          throw new Error(`Unexpected scope ${scope}`);
        }
        return {
          settings: {
            experimental: {
              gemmaModelRouter: userGemmaSettings,
            },
          },
        };
      }),
    };
  }

  beforeEach(() => {
    vi.clearAllMocks();
    mockLoadSettings.mockReturnValue(createMockSettings());
  });

  it('prefers the configured binary path from settings', () => {
    mockLoadSettings.mockReturnValue(
      createMockSettings({ binaryPath: '/custom/lit' }),
    );

    expect(getBinaryPath('lit.test')).toBe('/custom/lit');
  });

  it('ignores workspace overrides for the configured binary path', () => {
    mockLoadSettings.mockReturnValue(
      createMockSettings(
        { binaryPath: '/user/lit' },
        { binaryPath: '/workspace/evil' },
      ),
    );

    expect(getBinaryPath('lit.test')).toBe('/user/lit');
  });

  it('falls back to the default install location when no custom path is set', () => {
    expect(getBinaryPath('lit.test')).toBe(
      path.join(getLiteRtBinDir(), 'lit.test'),
    );
  });

  it('resolves the configured port and binary path from settings', () => {
    mockLoadSettings.mockReturnValue(
      createMockSettings(
        { binaryPath: '/custom/lit' },
        {
          enabled: true,
          classifier: {
            host: 'http://localhost:8123/v1beta',
          },
        },
      ),
    );

    expect(resolveGemmaConfig(9379)).toEqual({
      settingsEnabled: true,
      configuredPort: 8123,
      configuredBinaryPath: '/custom/lit',
    });
  });

  it('checks binary installation using the resolved binary path', () => {
    mockLoadSettings.mockReturnValue(
      createMockSettings({ binaryPath: '/custom/lit' }),
    );
    vi.spyOn(fs, 'existsSync').mockReturnValue(true);

    expect(isBinaryInstalled()).toBe(true);
    expect(fs.existsSync).toHaveBeenCalledWith('/custom/lit');
  });

  it('parses structured server process info from the pid file', () => {
    vi.spyOn(fs, 'readFileSync').mockReturnValue(
      JSON.stringify({
        pid: 1234,
        binaryPath: '/custom/lit',
        port: 8123,
      }),
    );

    expect(readServerProcessInfo()).toEqual({
      pid: 1234,
      binaryPath: '/custom/lit',
      port: 8123,
    });
  });

  it('parses legacy pid-only files for backward compatibility', () => {
    vi.spyOn(fs, 'readFileSync').mockReturnValue('4321');

    expect(readServerProcessInfo()).toEqual({
      pid: 4321,
    });
  });

  it('matches only the expected LiteRT serve command', () => {
    expect(
      isExpectedLiteRtServerCommand('/custom/lit serve --port=8123 --verbose', {
        binaryPath: '/custom/lit',
        port: 8123,
      }),
    ).toBe(true);

    expect(
      isExpectedLiteRtServerCommand('/custom/lit run --port=8123', {
        binaryPath: '/custom/lit',
        port: 8123,
      }),
    ).toBe(false);

    expect(
      isExpectedLiteRtServerCommand('/custom/lit serve --port=9000', {
        binaryPath: '/custom/lit',
        port: 8123,
      }),
    ).toBe(false);
  });
});