-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex-page.test.ts
More file actions
202 lines (183 loc) · 7.49 KB
/
index-page.test.ts
File metadata and controls
202 lines (183 loc) · 7.49 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { generateFromSchema, readDoc as readDocument } from '../utils';
import fs from 'node:fs';
import path from 'node:path';
import { beforeAll, describe, expect, it } from 'vitest';
describe('documentation plugin: index page', () => {
describe('index page structure', () => {
let tmpDir: string;
beforeAll(async () => {
tmpDir = await generateFromSchema(`
/// A registered user in the platform.
model User {
id String @id @default(cuid())
role Role
posts Post[]
}
/// Has a description.
model Post {
id String @id @default(cuid())
author User @relation(fields: [authorId], references: [id])
authorId String
}
/// Roles available in the system.
enum Role {
ADMIN
USER
}
/// Common timestamp fields.
type Timestamps {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
type Metadata {
version Int @default(1)
}
procedure getUser(id: String): User
mutation procedure signUp(name: String): User
`);
});
it('produces index.md with summary counts, TOC, and introductory context', () => {
const indexContent = readDocument(tmpDir, 'index.md');
expect(fs.existsSync(path.join(tmpDir, 'index.md'))).toBe(true);
expect(indexContent).toContain('# Schema Documentation');
expect(indexContent).toContain('2 models');
expect(indexContent).toContain('1 enum');
expect(indexContent).toContain('2 types');
expect(indexContent).toContain('2 procedures');
expect(indexContent).toContain('ZModel');
expect(indexContent).toContain('zenstack.dev');
expect(indexContent).toContain('[Models](#models)');
expect(indexContent).toContain('[Enums](#enums)');
expect(indexContent).toContain('[Types](#types)');
expect(indexContent).toContain('[Procedures](#procedures)');
const tocLine = indexContent
.split('\n')
.find((l) => l.includes('[Models](#models)'));
expect(tocLine).toContain('[Enums](#enums)');
});
it('lists entities alpha-sorted with links and inline descriptions', () => {
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('[Post](./models/Post.md)');
expect(indexContent).toContain('[User](./models/User.md)');
const postIndex = indexContent.indexOf('[Post]');
const userIndex = indexContent.indexOf('[User]');
expect(postIndex).toBeLessThan(userIndex);
expect(indexContent).toContain('A registered user in the platform');
expect(indexContent).toContain('Has a description');
expect(indexContent).toContain('Roles available in the system');
expect(indexContent).toContain('Common timestamp fields');
expect(indexContent).toContain('[Role](./enums/Role.md)');
expect(indexContent).toContain('[Metadata](./types/Metadata.md)');
expect(indexContent).toContain('[Timestamps](./types/Timestamps.md)');
const metaIndex = indexContent.indexOf('[Metadata]');
const tsIndex = indexContent.indexOf('[Timestamps]');
expect(metaIndex).toBeLessThan(tsIndex);
});
it('lists procedures with query/mutation distinction and links to relationships', () => {
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('[getUser](./procedures/getUser.md)');
expect(indexContent).toContain('[signUp](./procedures/signUp.md)');
expect(indexContent).toContain('query');
expect(indexContent).toContain('mutation');
expect(indexContent).toContain('[Relationships](./relationships.md)');
});
it('includes generation stats section', () => {
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('Generation Stats');
expect(indexContent).toContain('Files');
expect(indexContent).toContain('Duration');
expect(indexContent).toMatch(/\d+(\.\d+)?\s*ms/u);
});
});
it('omits generation stats on index.md when includeGenerationStats is false', async () => {
const tmpDir = await generateFromSchema(
`
model User {
id String @id @default(cuid())
}
`,
{ includeGenerationStats: false },
);
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).not.toContain('Generation Stats');
expect(indexContent).not.toContain('**Duration**');
});
it('lists views in a separate section from models', async () => {
const tmpDir = await generateFromSchema(`
model User {
id String @id @default(cuid())
}
view UserInfo {
id Int
email String
name String
}
view ActiveUsers {
id Int
count Int
}
`);
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('Views');
expect(indexContent).toContain('[ActiveUsers](./views/ActiveUsers.md)');
expect(indexContent).toContain('[UserInfo](./views/UserInfo.md)');
expect(indexContent).toContain('2 views');
expect(fs.existsSync(path.join(tmpDir, 'views', 'UserInfo.md'))).toBe(true);
});
it('uses custom title from plugin options', async () => {
const tmpDir = await generateFromSchema(
`
model User {
id String @id @default(cuid())
}
`,
{ title: 'My API' },
);
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('# My API');
expect(indexContent).not.toContain('# Schema Documentation');
});
it('deprecated model with description uses consistent em dash formatting', async () => {
const tmpDir = await generateFromSchema(`
/// Old user model.
model OldUser {
id String @id @default(cuid())
@@meta('doc:deprecated', 'Use UserV2 instead')
}
model UserV2 {
id String @id @default(cuid())
}
`);
const indexContent = readDocument(tmpDir, 'index.md');
expect(indexContent).toContain('~~[OldUser]');
expect(indexContent).toContain('*Deprecated: Use UserV2 instead*');
expect(indexContent).toContain('— Old user model');
expect(indexContent).not.toMatch(
/\*Deprecated: Use UserV2 instead\* Old user/u,
);
});
it('filesGenerated count includes SKILL.md when generateSkill is enabled', async () => {
const withoutSkill = await generateFromSchema(`
model User {
id String @id @default(cuid())
}
`);
const withSkill = await generateFromSchema(
`
model User {
id String @id @default(cuid())
}
`,
{ generateSkill: true },
);
const countWithout = readDocument(withoutSkill, 'index.md').match(
/\*\*Files\*\* \| (\d+)/u,
);
const countWith = readDocument(withSkill, 'index.md').match(
/\*\*Files\*\* \| (\d+)/u,
);
expect(countWithout).not.toBeNull();
expect(countWith).not.toBeNull();
expect(Number(countWith![1])).toBe(Number(countWithout![1]) + 1);
});
});