pi-interview 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +346 -0
- package/bin/install.js +87 -0
- package/form/index.html +119 -0
- package/form/script.js +2213 -0
- package/form/styles.css +1394 -0
- package/form/themes/default-dark.css +5 -0
- package/form/themes/default-light.css +24 -0
- package/form/themes/tufte-dark.css +29 -0
- package/form/themes/tufte-light.css +29 -0
- package/index.ts +354 -0
- package/package.json +31 -0
- package/schema.ts +236 -0
- package/server.ts +765 -0
- package/settings.ts +29 -0
package/schema.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
export interface CodeBlock {
|
|
2
|
+
code: string;
|
|
3
|
+
lang?: string;
|
|
4
|
+
file?: string;
|
|
5
|
+
lines?: string;
|
|
6
|
+
highlights?: number[];
|
|
7
|
+
title?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface RichOption {
|
|
11
|
+
label: string;
|
|
12
|
+
code?: CodeBlock;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type OptionValue = string | RichOption;
|
|
16
|
+
|
|
17
|
+
export interface Question {
|
|
18
|
+
id: string;
|
|
19
|
+
type: "single" | "multi" | "text" | "image";
|
|
20
|
+
question: string;
|
|
21
|
+
options?: OptionValue[];
|
|
22
|
+
recommended?: string | string[];
|
|
23
|
+
context?: string;
|
|
24
|
+
codeBlock?: CodeBlock;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface QuestionsFile {
|
|
28
|
+
title?: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
questions: Question[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getOptionLabel(option: OptionValue): string {
|
|
34
|
+
return typeof option === "string" ? option : option.label;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isRichOption(option: OptionValue): option is RichOption {
|
|
38
|
+
return typeof option === "object" && option !== null && "label" in option;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const SCHEMA_EXAMPLE = `Expected format:
|
|
42
|
+
{
|
|
43
|
+
"title": "Optional Title",
|
|
44
|
+
"questions": [
|
|
45
|
+
{ "id": "q1", "type": "single", "question": "Pick one?", "options": ["A", "B"] },
|
|
46
|
+
{ "id": "q2", "type": "multi", "question": "Pick many?", "options": ["X", "Y", "Z"] },
|
|
47
|
+
{ "id": "q3", "type": "text", "question": "Describe?" },
|
|
48
|
+
{ "id": "q4", "type": "image", "question": "Upload?" }
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
Valid types: single, multi, text, image
|
|
52
|
+
Options: array of strings or objects with { label, code? }`;
|
|
53
|
+
|
|
54
|
+
function validateCodeBlock(block: unknown, context: string): CodeBlock {
|
|
55
|
+
if (!block || typeof block !== "object") {
|
|
56
|
+
throw new Error(`${context}: codeBlock must be an object`);
|
|
57
|
+
}
|
|
58
|
+
const b = block as Record<string, unknown>;
|
|
59
|
+
if (typeof b.code !== "string") {
|
|
60
|
+
throw new Error(`${context}: codeBlock.code must be a string`);
|
|
61
|
+
}
|
|
62
|
+
if (b.lang !== undefined && typeof b.lang !== "string") {
|
|
63
|
+
throw new Error(`${context}: codeBlock.lang must be a string`);
|
|
64
|
+
}
|
|
65
|
+
if (b.file !== undefined && typeof b.file !== "string") {
|
|
66
|
+
throw new Error(`${context}: codeBlock.file must be a string`);
|
|
67
|
+
}
|
|
68
|
+
if (b.lines !== undefined && typeof b.lines !== "string") {
|
|
69
|
+
throw new Error(`${context}: codeBlock.lines must be a string`);
|
|
70
|
+
}
|
|
71
|
+
if (b.title !== undefined && typeof b.title !== "string") {
|
|
72
|
+
throw new Error(`${context}: codeBlock.title must be a string`);
|
|
73
|
+
}
|
|
74
|
+
if (b.highlights !== undefined) {
|
|
75
|
+
if (!Array.isArray(b.highlights) || b.highlights.some((h) => typeof h !== "number")) {
|
|
76
|
+
throw new Error(`${context}: codeBlock.highlights must be an array of numbers`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return b as unknown as CodeBlock;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function validateOption(option: unknown, questionId: string, index: number): OptionValue {
|
|
83
|
+
if (typeof option === "string") {
|
|
84
|
+
return option;
|
|
85
|
+
}
|
|
86
|
+
if (option && typeof option === "object") {
|
|
87
|
+
const o = option as Record<string, unknown>;
|
|
88
|
+
if (typeof o.label !== "string") {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Question "${questionId}": option at index ${index} must have a "label" string`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (o.code !== undefined) {
|
|
94
|
+
validateCodeBlock(o.code, `Question "${questionId}" option "${o.label}"`);
|
|
95
|
+
}
|
|
96
|
+
return option as RichOption;
|
|
97
|
+
}
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Question "${questionId}": option at index ${index} must be a string or object with label`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function validateBasicStructure(data: unknown): QuestionsFile {
|
|
104
|
+
if (Array.isArray(data)) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Invalid questions file: root must be an object, not an array.\n\n${SCHEMA_EXAMPLE}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!data || typeof data !== "object") {
|
|
111
|
+
throw new Error(`Invalid questions file: must be an object.\n\n${SCHEMA_EXAMPLE}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const obj = data as Record<string, unknown>;
|
|
115
|
+
|
|
116
|
+
if (("label" in obj || "description" in obj) && !("questions" in obj)) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`Invalid questions file: missing "questions" array. Did you mean to wrap your questions?\n\n${SCHEMA_EXAMPLE}`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (obj.title !== undefined && typeof obj.title !== "string") {
|
|
123
|
+
throw new Error("Invalid questions file: title must be a string");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (obj.description !== undefined && typeof obj.description !== "string") {
|
|
127
|
+
throw new Error("Invalid questions file: description must be a string");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!Array.isArray(obj.questions) || obj.questions.length === 0) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Invalid questions file: "questions" must be a non-empty array.\n\n${SCHEMA_EXAMPLE}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const validTypes = ["single", "multi", "text", "image"];
|
|
137
|
+
for (let i = 0; i < obj.questions.length; i++) {
|
|
138
|
+
const q = obj.questions[i] as Record<string, unknown>;
|
|
139
|
+
if (!q || typeof q !== "object") {
|
|
140
|
+
throw new Error(`Invalid question at index ${i}: must be an object`);
|
|
141
|
+
}
|
|
142
|
+
if (typeof q.id !== "string") {
|
|
143
|
+
throw new Error(`Invalid question at index ${i}: id must be a string`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (typeof q.type !== "string" || !validTypes.includes(q.type)) {
|
|
147
|
+
const hint = q.type === "select" ? ' (use "single" instead of "select")' : "";
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Question "${q.id}": type must be one of: ${validTypes.join(", ")}${hint}`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (typeof q.question !== "string") {
|
|
154
|
+
const hint = "label" in q || "description" in q
|
|
155
|
+
? ' (use "question" field, not "label" or "description")'
|
|
156
|
+
: "";
|
|
157
|
+
throw new Error(`Question "${q.id}": "question" field must be a string${hint}`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (q.options !== undefined) {
|
|
161
|
+
if (!Array.isArray(q.options) || q.options.length === 0) {
|
|
162
|
+
throw new Error(`Question "${q.id}": options must be a non-empty array`);
|
|
163
|
+
}
|
|
164
|
+
for (let j = 0; j < q.options.length; j++) {
|
|
165
|
+
validateOption(q.options[j], q.id as string, j);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (q.context !== undefined && typeof q.context !== "string") {
|
|
170
|
+
throw new Error(`Question "${q.id}": context must be a string`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (q.codeBlock !== undefined) {
|
|
174
|
+
validateCodeBlock(q.codeBlock, `Question "${q.id}"`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return obj as unknown as QuestionsFile;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function validateQuestions(data: unknown): QuestionsFile {
|
|
182
|
+
const parsed = validateBasicStructure(data);
|
|
183
|
+
|
|
184
|
+
const ids = new Set<string>();
|
|
185
|
+
for (const q of parsed.questions) {
|
|
186
|
+
if (ids.has(q.id)) {
|
|
187
|
+
throw new Error(`Duplicate question id: "${q.id}"`);
|
|
188
|
+
}
|
|
189
|
+
ids.add(q.id);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const q of parsed.questions) {
|
|
193
|
+
if (q.type === "single" || q.type === "multi") {
|
|
194
|
+
if (!q.options || q.options.length === 0) {
|
|
195
|
+
throw new Error(`Question "${q.id}": options required for type "${q.type}"`);
|
|
196
|
+
}
|
|
197
|
+
} else if (q.type === "text" || q.type === "image") {
|
|
198
|
+
if (q.options) {
|
|
199
|
+
throw new Error(`Question "${q.id}": options not allowed for type "${q.type}"`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (q.recommended !== undefined) {
|
|
204
|
+
if (q.type === "text" || q.type === "image") {
|
|
205
|
+
throw new Error(`Question "${q.id}": recommended not allowed for type "${q.type}"`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const optionLabels = q.options?.map(getOptionLabel) ?? [];
|
|
209
|
+
|
|
210
|
+
if (q.type === "single") {
|
|
211
|
+
if (typeof q.recommended !== "string") {
|
|
212
|
+
throw new Error(`Question "${q.id}": recommended must be string for single-select`);
|
|
213
|
+
}
|
|
214
|
+
if (!optionLabels.includes(q.recommended)) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`Question "${q.id}": recommended "${q.recommended}" not in options`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (q.type === "multi") {
|
|
222
|
+
const recs = Array.isArray(q.recommended) ? q.recommended : [q.recommended];
|
|
223
|
+
for (const rec of recs) {
|
|
224
|
+
if (!optionLabels.includes(rec)) {
|
|
225
|
+
throw new Error(`Question "${q.id}": recommended "${rec}" not in options`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (!Array.isArray(q.recommended)) {
|
|
229
|
+
q.recommended = recs;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return parsed;
|
|
236
|
+
}
|