Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

transform.test.ts

102 lines3.2 KB

1import { readFileSync } from 'node:fs'
2import { dirname, resolve } from 'node:path'
3import { fileURLToPath } from 'node:url'
4import jscodeshift from 'jscodeshift'
5import { describe, expect, it } from 'vitest'
6import transform from './transform'
7​
8const __filename = fileURLToPath(import.meta.url)
9const __dirname = dirname(__filename)
10const FIXTURES = resolve(__dirname, '__testfixtures__')
11​
12function read(name: string): string {
13 return readFileSync(resolve(FIXTURES, name), 'utf-8')
15​
16function runTransform(
17 fixtureBaseName: string,
18 ext: 'ts' | 'tsx',
19): { output: string; reports: Array<string> } {
20 const source = read(`${fixtureBaseName}.input.${ext}`)
21 const reports: Array<string> = []
22 const j = jscodeshift.withParser('tsx')
23 const result = transform(
24 { path: `${fixtureBaseName}.input.${ext}`, source },
25 {
26 jscodeshift: j,
27 j,
28 stats: () => {},
29 report: (msg: string) => {
30 reports.push(msg)
31 },
32 },
33 {},
34 )
35 if (typeof result !== 'string') {
36 throw new Error(
37 `transform returned ${typeof result} for ${fixtureBaseName}.input.${ext}; expected a string`,
38 )
39 }
40 return { output: result, reports }
42​
43// Normalize line endings — fixtures may be saved as CRLF on Windows
44// while jscodeshift emits LF, which would make a string compare
45// fail despite identical content.
46function normalize(s: string): string {
47 return s.replace(/\r\n/g, '\n').trim()
49​
50function expectFixture(
51 name: string,
52 ext: 'ts' | 'tsx' = 'ts',
53): { reports: Array<string> } {
54 const expected = read(`${name}.output.${ext}`)
55 const { output, reports } = runTransform(name, ext)
56 expect(normalize(output)).toBe(normalize(expected))
57 return { reports }
59​
60describe('ag-ui-compliance codemod', () => {
61 it('renames useChat({ body }) to useChat({ forwardedProps })', () => {
62 expectFixture('use-chat-body', 'tsx')
63 })
64​
65 it('expands a shorthand `body` property to `forwardedProps: body` (preserves the original identifier reference)', () => {
66 expectFixture('shorthand-body', 'tsx')
67 })
68​
69 it('renames body on ChatClient constructor and updateOptions calls', () => {
70 expectFixture('chat-client-body')
71 })
72​
73 it('renames Svelte chat.updateBody() to chat.updateForwardedProps()', () => {
74 expectFixture('svelte-update-body')
75 })
76​
77 it('renames chat({ conversationId }) to chat({ threadId })', () => {
78 expectFixture('chat-conversation-id')
79 })
80​
81 it('leaves files without TanStack AI imports untouched', () => {
82 expectFixture('no-imports')
83 })
84​
85 it('leaves objects that already declare both keys untouched, and reports the conflict for human resolution', () => {
86 const { reports } = expectFixture('conflict-leave-alone')
87 // The codemod must surface conflicts via api.report so users can
88 // find and merge them; silently leaving them alone hides intentional
89 // (or accidental) dual-key state from the migration audit.
90 expect(reports.length).toBeGreaterThan(0)
91 expect(
92 reports.some(
93 (r) => r.includes('useChat({ body })') && r.includes('left alone'),
94 ),
95 ).toBe(true)
96 })
97​
98 it('emits no report messages for clean transforms', () => {
99 const { reports } = expectFixture('use-chat-body', 'tsx')
100 expect(reports).toEqual([])
101 })
102})