You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.7 KiB
90 lines
3.7 KiB
import { describe, expect, it } from 'vitest'
|
|
import { readFileSync, readdirSync, existsSync } from 'node:fs'
|
|
import { join, resolve, dirname, relative } from 'node:path'
|
|
import ts from 'typescript'
|
|
|
|
function source(file: string): ts.SourceFile {
|
|
return ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true)
|
|
}
|
|
const directory = 'src/main/modules'
|
|
const modules = [
|
|
...readdirSync(directory)
|
|
.filter((name) => name.endsWith('.ts') && name !== 'index.ts')
|
|
.map((name) => join(directory, name)),
|
|
join(directory, 'notes/index.ts')
|
|
]
|
|
|
|
describe('direct tRPC module boundaries', () => {
|
|
it('keeps renderer pages as feature composition without direct RPC or task state', () => {
|
|
for (const name of readdirSync('src/renderer/src/pages')) {
|
|
const page = readFileSync(join('src/renderer/src/pages', name), 'utf8')
|
|
expect(page, name).not.toMatch(/lib\/trpc|lib\/useTask|\.subscribe\(|onMounted|onUnmounted/)
|
|
}
|
|
})
|
|
it('keeps shared models independent of server code and API registration', () => {
|
|
for (const name of readdirSync('packages/contracts/src')) {
|
|
const file = source(join('packages/contracts/src', name))
|
|
for (const statement of file.statements) {
|
|
if (ts.isImportDeclaration(statement))
|
|
expect((statement.moduleSpecifier as ts.StringLiteral).text, name).toMatch(
|
|
/^(zod|\.\/\w+)$/
|
|
)
|
|
}
|
|
expect(file.text, name).not.toMatch(
|
|
/initTRPC|ipc\.register|export const (routes|eventSchemas|windowPermissions)/
|
|
)
|
|
}
|
|
expect(existsSync('packages/contracts/src/registry.ts')).toBe(false)
|
|
expect(existsSync('packages/trpc/package.json')).toBe(false)
|
|
})
|
|
|
|
it('keeps routers beside feature code without imports of sibling modules', () => {
|
|
for (const file of modules) {
|
|
for (const statement of source(file).statements) {
|
|
if (!ts.isImportDeclaration(statement)) continue
|
|
const dependency = (statement.moduleSpecifier as ts.StringLiteral).text
|
|
expect(dependency, file).not.toBe('@template/contracts')
|
|
if (dependency.startsWith('.')) {
|
|
const feature = relative(resolve(directory), resolve(dirname(file), dependency))
|
|
if (!feature.startsWith('..')) {
|
|
const ownFeature = relative(resolve(directory), resolve(file))
|
|
.split(/[\\/]/)[0]!
|
|
.replace(/\.ts$/, '')
|
|
expect(feature.split(/[\\/]/)[0]!.replace(/\.ts$/, ''), file).toBe(ownFeature)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
it('validates outputs in every query and mutation instead of a separate dispatch table', () => {
|
|
let procedures = 0
|
|
for (const file of modules) {
|
|
const tree = source(file)
|
|
const visit = (node: ts.Node): void => {
|
|
if (
|
|
ts.isCallExpression(node) &&
|
|
ts.isPropertyAccessExpression(node.expression) &&
|
|
['query', 'mutation'].includes(node.expression.name.text)
|
|
) {
|
|
expect(node.expression.expression.getText(tree), file).toContain('.output(')
|
|
procedures++
|
|
}
|
|
ts.forEachChild(node, visit)
|
|
}
|
|
visit(tree)
|
|
expect(tree.text, file).not.toMatch(/ipc\.register|ctx\.invoke/)
|
|
}
|
|
expect(procedures).toBeGreaterThan(20)
|
|
})
|
|
|
|
it('allows renderer to import only the main router type', () => {
|
|
const file = source('src/renderer/src/lib/trpc.ts')
|
|
const imports = file.statements.filter(ts.isImportDeclaration)
|
|
const mainImport = imports.find((statement) =>
|
|
(statement.moduleSpecifier as ts.StringLiteral).text.includes('/main/')
|
|
)!
|
|
expect(mainImport.importClause?.isTypeOnly).toBe(true)
|
|
expect(mainImport.importClause?.namedBindings?.getText(file)).toBe('{ AppRouter }')
|
|
})
|
|
})
|
|
|