Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { dirname, resolve } from 'node:path'
3import { fileURLToPath } from 'node:url'
4import jscodeshift from 'jscodeshift'
5import * as prettier from 'prettier'
6import { describe, expect, it } from 'vitest'
7import transform from './transform'
8
9const __filename = fileURLToPath(import.meta.url)
10const __dirname = dirname(__filename)
11const FIXTURES = resolve(__dirname, '__testfixtures__')
12
13function read(name: string): string {
14 return readFileSync(resolve(FIXTURES, name), 'utf-8')
15}
16
17function runTransform(
18 fixtureBaseName: string,
19 ext: 'ts' | 'tsx',
20): { output: string; reports: Array<string> } {
21 const source = read(`${fixtureBaseName}.input.${ext}`)
22 const reports: Array<string> = []
23 const j = jscodeshift.withParser('tsx')
24 const result = transform(
25 { path: `${fixtureBaseName}.input.${ext}`, source },
26 {
27 jscodeshift: j,
28 j,
29 stats: () => {},
30 report: (msg: string) => {
31 reports.push(msg)
32 },
33 },
34 {},
35 )
36 if (typeof result !== 'string') {
37 throw new Error(
38 `transform returned ${typeof result} for ${fixtureBaseName}.input.${ext}; expected a string`,
39 )
40 }
41 return { output: result, reports }
42}
43
44// Run both sides through Prettier before comparing. recast (jscodeshift's
45// printer) emits semicolons and drops trailing commas, while the fixtures are
46// written in the repo's Prettier style. Formatting both with the same options
47// makes the comparison about semantics, not print quirks, and keeps the
48// `.output.ts` fixtures readable/Prettier-clean. Prettier also normalizes line
49// endings (CRLF on Windows → LF).
50async function normalize(s: string): Promise<string> {
51 const formatted = await prettier.format(s, { parser: 'typescript' })
52 return formatted.trim()
53}
54
55async function expectFixture(
56 name: string,
57 ext: 'ts' | 'tsx' = 'ts',
58): Promise<{ reports: Array<string> }> {
59 const expected = read(`${name}.output.${ext}`)
60 const { output, reports } = runTransform(name, ext)
61 expect(await normalize(output)).toBe(await normalize(expected))
62 return { reports }
63}
64
65describe('move-sampling-to-model-options codemod', () => {
66 it('moves openai temperature/maxTokens into modelOptions (renamed)', async () => {
67 const { reports } = await expectFixture('openai-basic')
68 expect(reports).toEqual([])
69 })
70
71 it('renames gemini topP/maxTokens to topP/maxOutputTokens', async () => {
72 await expectFixture('gemini-rename')
73 })
74
75 it('renames groq maxTokens to max_completion_tokens', async () => {
76 await expectFixture('groq-maxtokens')
77 })
78
79 it('renames openrouter maxTokens to maxCompletionTokens', async () => {
80 await expectFixture('openrouter-maxtokens')
81 })
82
83 it('nests ollama sampling options inside modelOptions.options', async () => {
84 await expectFixture('ollama-nested')
85 })
86
87 it('merges into an existing modelOptions object literal', async () => {
88 await expectFixture('anthropic-merge')
89 })
90
91 it('expands a shorthand sampling prop to `key: identifier`', async () => {
92 await expectFixture('shorthand')
93 })
94
95 it('transforms createChatOptions() calls', async () => {
96 await expectFixture('create-chat-options')
97 })
98
99 it('transforms ai() and generate() callee variants', async () => {
100 await expectFixture('generate-and-ai')
101 })
102
103 it('leaves files without a @tanstack/ai helper import untouched', async () => {
104 const { reports } = await expectFixture('no-import')
105 expect(reports).toEqual([])
106 })
107
108 it('leaves a call alone and reports when a target key already exists in modelOptions', async () => {
109 const { reports } = await expectFixture('conflict')
110 expect(reports.length).toBeGreaterThan(0)
111 expect(
112 reports.some(
113 (r) =>
114 r.includes('a target key already exists') && r.includes('left alone'),
115 ),
116 ).toBe(true)
117 })
118
119 it('leaves a call alone and reports when the adapter is unresolvable', async () => {
120 const { reports } = await expectFixture('unresolvable-adapter')
121 expect(reports.length).toBeGreaterThan(0)
122 expect(
123 reports.some(
124 (r) =>
125 r.includes('could not resolve a known provider adapter') &&
126 r.includes('left alone'),
127 ),
128 ).toBe(true)
129 })
130})