-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtools.test.ts
More file actions
160 lines (138 loc) · 5.84 KB
/
tools.test.ts
File metadata and controls
160 lines (138 loc) · 5.84 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { describe, test, expect } from "bun:test"
// Standalone test for training tool parameter validation and logic
// Mirrors the schemas from the training tools without importing from src/
// to avoid dependency chain issues.
// Import only from training/types which has minimal dependencies
import {
TrainingKind,
trainingId,
trainingTags,
embedTrainingMeta,
parseTrainingMeta,
TRAINING_MAX_PATTERNS_PER_KIND,
type TrainingBlockMeta,
} from "../../src/altimate/training/types"
describe("training_save parameter validation", () => {
// Validate name format manually (mirrors the regex in training-save.ts)
const NAME_REGEX = /^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$/
test("accepts valid names", () => {
expect(NAME_REGEX.test("staging-model")).toBe(true)
expect(NAME_REGEX.test("no-float")).toBe(true)
expect(NAME_REGEX.test("arr")).toBe(true)
expect(NAME_REGEX.test("sql-style-v2")).toBe(true)
expect(NAME_REGEX.test("staging-model_v2")).toBe(true)
expect(NAME_REGEX.test("a")).toBe(true)
expect(NAME_REGEX.test("a1")).toBe(true)
})
test("rejects invalid names", () => {
expect(NAME_REGEX.test("")).toBe(false)
expect(NAME_REGEX.test("MyRule")).toBe(false)
expect(NAME_REGEX.test("my rule")).toBe(false)
expect(NAME_REGEX.test("-invalid")).toBe(false)
expect(NAME_REGEX.test("invalid-")).toBe(false)
expect(NAME_REGEX.test("_invalid")).toBe(false)
expect(NAME_REGEX.test("invalid_")).toBe(false)
expect(NAME_REGEX.test("foo/bar")).toBe(false)
expect(NAME_REGEX.test("foo.bar")).toBe(false)
})
test("kind validation via zod schema", () => {
expect(TrainingKind.safeParse("pattern").success).toBe(true)
expect(TrainingKind.safeParse("rule").success).toBe(true)
expect(TrainingKind.safeParse("glossary").success).toBe(true)
expect(TrainingKind.safeParse("standard").success).toBe(true)
expect(TrainingKind.safeParse("invalid").success).toBe(false)
expect(TrainingKind.safeParse("").success).toBe(false)
expect(TrainingKind.safeParse(123).success).toBe(false)
})
})
describe("training ID generation", () => {
test("generates correct IDs for all kinds", () => {
expect(trainingId("pattern", "test")).toBe("training/pattern/test")
expect(trainingId("rule", "test")).toBe("training/rule/test")
expect(trainingId("glossary", "test")).toBe("training/glossary/test")
expect(trainingId("standard", "test")).toBe("training/standard/test")
})
test("handles names with hyphens", () => {
expect(trainingId("pattern", "staging-model")).toBe("training/pattern/staging-model")
})
test("handles names with underscores", () => {
expect(trainingId("rule", "no_float")).toBe("training/rule/no_float")
})
})
describe("training tags generation", () => {
test("includes training tag and kind for all kinds", () => {
for (const kind of ["pattern", "rule", "glossary", "standard"] as const) {
const tags = trainingTags(kind)
expect(tags).toContain("training")
expect(tags).toContain(kind)
expect(tags.length).toBe(2)
}
})
test("includes extra tags when provided", () => {
const tags = trainingTags("rule", ["sql", "naming"])
expect(tags).toContain("training")
expect(tags).toContain("rule")
expect(tags).toContain("sql")
expect(tags).toContain("naming")
expect(tags.length).toBe(4)
})
})
describe("training meta roundtrip through content", () => {
test("embeds and parses meta correctly", () => {
const meta: TrainingBlockMeta = {
kind: "pattern",
source: "stg_orders.sql",
applied: 5,
}
const content = "- Use CTEs\n- Cast types"
const embedded = embedTrainingMeta(content, meta)
const parsed = parseTrainingMeta(embedded)
expect(parsed).toBeDefined()
expect(parsed!.kind).toBe("pattern")
expect(parsed!.source).toBe("stg_orders.sql")
expect(parsed!.applied).toBe(5)
})
test("preserves content after embedding meta", () => {
const content = "Rule: Use NUMERIC(18,2)\n\nDetails:\n- For all *_amount columns"
const meta: TrainingBlockMeta = { kind: "rule", applied: 0 }
const embedded = embedTrainingMeta(content, meta)
expect(embedded).toContain("Rule: Use NUMERIC(18,2)")
expect(embedded).toContain("- For all *_amount columns")
})
test("replaces existing meta on re-embed", () => {
const meta1: TrainingBlockMeta = { kind: "pattern", applied: 1 }
const meta2: TrainingBlockMeta = { kind: "pattern", applied: 10 }
const content = "Pattern content"
const embedded1 = embedTrainingMeta(content, meta1)
expect(parseTrainingMeta(embedded1)!.applied).toBe(1)
const embedded2 = embedTrainingMeta(embedded1, meta2)
expect(parseTrainingMeta(embedded2)!.applied).toBe(10)
// Should not have duplicate meta blocks
const metaBlocks = embedded2.match(/<!-- training/g)
expect(metaBlocks).toHaveLength(1)
})
test("handles content with special characters", () => {
const content = "Use `{{ source('schema', 'table') }}` macro\n<!-- not training -->"
const meta: TrainingBlockMeta = { kind: "pattern", applied: 0 }
const embedded = embedTrainingMeta(content, meta)
expect(embedded).toContain("{{ source('schema', 'table') }}")
expect(embedded).toContain("<!-- not training -->")
})
})
describe("TRAINING_MAX_PATTERNS_PER_KIND", () => {
test("is a reasonable limit", () => {
expect(TRAINING_MAX_PATTERNS_PER_KIND).toBe(20)
expect(TRAINING_MAX_PATTERNS_PER_KIND).toBeGreaterThan(0)
expect(TRAINING_MAX_PATTERNS_PER_KIND).toBeLessThanOrEqual(50)
})
})
describe("content length validation", () => {
test("content within 2500 chars is acceptable", () => {
const content = "x".repeat(2500)
expect(content.length).toBeLessThanOrEqual(2500)
})
test("content over 2500 chars should be rejected by tool", () => {
const content = "x".repeat(2501)
expect(content.length).toBeGreaterThan(2500)
})
})