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

root-declaration-consumer.test.ts

89 lines2.9 KB

1import { readdirSync, readFileSync } from 'node:fs'
2import { join } from 'node:path'
3import { describe, expect, it } from 'vitest'
4​
5const declarationsRoot = join(import.meta.dirname, '..', 'dist', 'esm')
6const sourceRoot = join(import.meta.dirname, '..', 'src')
7​
8const prohibitedProductionTestSeams = [
9 'memorySandboxSnapshotsForTest',
10 'MemorySandboxSnapshotsTestOptions',
11 'failAfterTranscriptStage',
12 'failAfterCheckpointStage',
13 'failAfterReferenceStage',
14 'failAfterHeadStage',
15 'createInMemoryCheckpointCoordinator',
16 'InMemoryCheckpointCoordinator',
17 'PreparedCheckpointFork',
19​
20function filesUnder(directory: string, extension: string): Array<string> {
21 return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
22 const path = join(directory, entry.name)
23 if (entry.isDirectory()) return filesUnder(path, extension)
24 return entry.isFile() && path.endsWith(extension) ? [path] : []
25 })
27​
28function rootDeclarationGraph(entry: string): Array<string> {
29 const seen = new Set<string>()
30 const visit = (file: string): void => {
31 if (seen.has(file)) return
32 seen.add(file)
33 const source = readFileSync(file, 'utf8')
34 for (const specifier of source.matchAll(/from ['"](\.\/[^'"]+)['"]/g)) {
35 const moduleSpecifier = specifier[1]
36 if (moduleSpecifier === undefined) continue
37 const imported = join(file, '..', moduleSpecifier)
38 const declaration = imported.endsWith('.d.ts')
39 ? imported
40 : imported.endsWith('.js')
41 ? imported.slice(0, -3) + '.d.ts'
42 : `${imported}.d.ts`
43 visit(declaration)
44 }
45 }
46 visit(entry)
47 return [...seen]
49​
50describe('root declarations', () => {
51 function expectNoPersistenceDeclarationReference(entry: string): void {
52 const files = rootDeclarationGraph(entry)
53 expect(files.length).toBeGreaterThan(0)
54​
55 const persistenceSpecifiers = files.flatMap((file) => {
56 const source = readFileSync(file, 'utf8')
57 return source.includes('@tanstack/ai-persistence') ? [file] : []
58 })
59​
60 expect(persistenceSpecifiers).toEqual([])
61 }
62​
63 it('do not require persistence type resolution', () => {
64 expectNoPersistenceDeclarationReference(
65 join(declarationsRoot, 'index.d.ts'),
66 )
67 })
68​
69 it('ships a testkit declaration graph that does not require persistence', () => {
70 expectNoPersistenceDeclarationReference(
71 join(declarationsRoot, 'testkit', 'conformance.d.ts'),
72 )
73 })
74​
75 it('do not contain production test seams in source or emitted declarations', () => {
76 const files = [
77 ...filesUnder(sourceRoot, '.ts'),
78 ...filesUnder(declarationsRoot, '.d.ts'),
79 ]
80 const matches = files.flatMap((file) => {
81 const source = readFileSync(file, 'utf8')
82 return prohibitedProductionTestSeams.flatMap((seam) =>
83 source.includes(seam) ? [`${file}: ${seam}`] : [],
84 )
85 })
86​
87 expect(matches).toEqual([])
88 })
89})