File size: 3,592 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { ModelConfigService } from './modelConfigService.js';
import { DEFAULT_MODEL_CONFIGS } from '../config/defaultModelConfigs.js';

const GOLDEN_FILE_PATH = path.resolve(
  process.cwd(),
  'src',
  'services',
  'test-data',
  'resolved-aliases.golden.json',
);

const RETRY_GOLDEN_FILE_PATH = path.resolve(
  process.cwd(),
  'src',
  'services',
  'test-data',
  'resolved-aliases-retry.golden.json',
);

describe('ModelConfigService Golden Test', () => {
  it('should match the golden file for resolved default aliases', async () => {
    const service = new ModelConfigService(DEFAULT_MODEL_CONFIGS);
    const aliases = Object.keys(DEFAULT_MODEL_CONFIGS.aliases ?? {});

    const resolvedAliases: Record<string, unknown> = {};
    for (const alias of aliases) {
      resolvedAliases[alias] =
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (service as any).internalGetResolvedConfig({ model: alias });
    }

    if (process.env['UPDATE_GOLDENS']) {
      await fs.mkdir(path.dirname(GOLDEN_FILE_PATH), { recursive: true });
      await fs.writeFile(
        GOLDEN_FILE_PATH,
        JSON.stringify(resolvedAliases, null, 2),
        'utf-8',
      );
      // In update mode, we pass the test after writing the file.
      return;
    }

    let goldenContent: string;
    try {
      goldenContent = await fs.readFile(GOLDEN_FILE_PATH, 'utf-8');
    } catch (e) {
      if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
        throw new Error(
          'Golden file not found. Run with `UPDATE_GOLDENS=true` to create it.',
        );
      }
      throw e;
    }

    const goldenData = JSON.parse(goldenContent);

    expect(
      resolvedAliases,
      'Golden file mismatch. If the new resolved aliases are correct, run the test with `UPDATE_GOLDENS=true` to regenerate the golden file.',
    ).toEqual(goldenData);
  });

  it('should match the golden file for resolved default aliases with isRetry=true', async () => {
    const service = new ModelConfigService(DEFAULT_MODEL_CONFIGS);
    const aliases = Object.keys(DEFAULT_MODEL_CONFIGS.aliases ?? {});

    const resolvedAliases: Record<string, unknown> = {};
    for (const alias of aliases) {
      resolvedAliases[alias] =
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (service as any).internalGetResolvedConfig({
          model: alias,
          isRetry: true,
        });
    }

    if (process.env['UPDATE_GOLDENS']) {
      await fs.mkdir(path.dirname(RETRY_GOLDEN_FILE_PATH), { recursive: true });
      await fs.writeFile(
        RETRY_GOLDEN_FILE_PATH,
        JSON.stringify(resolvedAliases, null, 2),
        'utf-8',
      );
      // In update mode, we pass the test after writing the file.
      return;
    }

    let goldenContent: string;
    try {
      goldenContent = await fs.readFile(RETRY_GOLDEN_FILE_PATH, 'utf-8');
    } catch (e) {
      if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
        throw new Error(
          'Golden file not found. Run with `UPDATE_GOLDENS=true` to create it.',
        );
      }
      throw e;
    }

    const goldenData = JSON.parse(goldenContent);

    expect(
      resolvedAliases,
      'Golden file mismatch. If the new resolved aliases are correct, run the test with `UPDATE_GOLDENS=true` to regenerate the golden file.',
    ).toEqual(goldenData);
  });
});