te.js 2.0.0 → 2.0.3
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 +25 -0
- package/docs/README.md +1 -1
- package/docs/getting-started.md +34 -0
- package/package.json +13 -2
- package/.cursor/plans/ai_native_framework_features_5bb1a20a.plan.md +0 -234
- package/.cursor/plans/auto_error_fix_agent_e68979c5.plan.md +0 -356
- package/.cursor/plans/tejas_framework_test_suite_5e3c6fad.plan.md +0 -168
- package/.prettierignore +0 -31
- package/.prettierrc +0 -5
- package/example/API_OVERVIEW.md +0 -77
- package/example/README.md +0 -155
- package/example/index.js +0 -29
- package/example/middlewares/auth.js +0 -9
- package/example/middlewares/global.midair.js +0 -6
- package/example/openapi.json +0 -390
- package/example/package.json +0 -23
- package/example/services/cache.service.js +0 -25
- package/example/services/user.service.js +0 -42
- package/example/start-redis.js +0 -2
- package/example/targets/cache.target.js +0 -35
- package/example/targets/index.target.js +0 -16
- package/example/targets/users.target.js +0 -60
- package/example/tejas.config.json +0 -22
- package/tests/auto-docs/handler-analyzer.test.js +0 -44
- package/tests/auto-docs/openapi-generator.test.js +0 -103
- package/tests/auto-docs/parse.test.js +0 -63
- package/tests/auto-docs/source-resolver.test.js +0 -58
- package/tests/helpers/index.js +0 -37
- package/tests/helpers/mock-http.js +0 -342
- package/tests/helpers/test-utils.js +0 -446
- package/tests/setup.test.js +0 -148
- package/vitest.config.js +0 -54
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: Auto Error Fix Agent
|
|
3
|
-
overview: When code hits an error (test failure or runtime exception), trigger an AI agent that analyzes the error, attempts a fix, and creates a PR on GitHub for review.
|
|
4
|
-
todos: []
|
|
5
|
-
isProject: false
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# Auto Error Fix Agent Plan
|
|
9
|
-
|
|
10
|
-
## Overview
|
|
11
|
-
|
|
12
|
-
When an error occurs—either a **test failure** or a **runtime exception**—an AI agent is triggered to:
|
|
13
|
-
|
|
14
|
-
1. Analyze the error (stack trace, context, failing code)
|
|
15
|
-
2. Propose and apply a fix
|
|
16
|
-
3. Create a branch, commit, push, and open a PR on GitHub
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## Trigger Modes
|
|
21
|
-
|
|
22
|
-
| Mode | When | Use Case |
|
|
23
|
-
| --------------------- | ------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
|
|
24
|
-
| **Test failure** | `npm run test:fix` or `tejas auto-fix` runs tests; on failure, agent triggers | Local dev, CI pipeline |
|
|
25
|
-
| **Runtime exception** | Error caught in [server/handler.js](server/handler.js) `errorHandler`; if `AUTO_ERROR_FIX` enabled, enqueue agent (async) | Dev server only |
|
|
26
|
-
|
|
27
|
-
**Recommendation:** Start with **test failure** mode. It's deterministic, reproducible, and doesn't add latency to request handling. Runtime mode can be added later as opt-in for dev.
|
|
28
|
-
|
|
29
|
-
---
|
|
30
|
-
|
|
31
|
-
## Architecture
|
|
32
|
-
|
|
33
|
-
```mermaid
|
|
34
|
-
flowchart TB
|
|
35
|
-
subgraph Trigger [Trigger]
|
|
36
|
-
Tests[Vitest Run]
|
|
37
|
-
Runtime[Runtime Error]
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
subgraph Agent [AI Agent]
|
|
41
|
-
Capture[Capture Context]
|
|
42
|
-
Analyze[Analyze Error]
|
|
43
|
-
Propose[Propose Fix]
|
|
44
|
-
Apply[Apply Changes]
|
|
45
|
-
Verify[Run Tests Again]
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
subgraph GitHub [GitHub]
|
|
49
|
-
Branch[Create Branch]
|
|
50
|
-
Commit[Commit]
|
|
51
|
-
Push[Push]
|
|
52
|
-
PR[Open PR]
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
Tests -->|"fail"| Capture
|
|
56
|
-
Runtime -->|"AUTO_ERROR_FIX"| Capture
|
|
57
|
-
|
|
58
|
-
Capture --> ShouldTrigger{shouldTrigger?}
|
|
59
|
-
ShouldTrigger -->|"no"| Skip[Skip - log only]
|
|
60
|
-
ShouldTrigger -->|"yes"| Analyze
|
|
61
|
-
Analyze --> Propose
|
|
62
|
-
Propose --> Apply
|
|
63
|
-
Apply --> Verify
|
|
64
|
-
Verify -->|"pass"| Branch
|
|
65
|
-
Verify -->|"fail"| Log[Log / Notify]
|
|
66
|
-
Branch --> Commit
|
|
67
|
-
Commit --> Push
|
|
68
|
-
Push --> PR
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
---
|
|
72
|
-
|
|
73
|
-
## Components
|
|
74
|
-
|
|
75
|
-
### 1. Error Context Capture
|
|
76
|
-
|
|
77
|
-
**Inputs to collect:**
|
|
78
|
-
|
|
79
|
-
- Error message and stack trace
|
|
80
|
-
- Failing test name and file (if test failure)
|
|
81
|
-
- Test output (Vitest JSON reporter or stdout)
|
|
82
|
-
- Relevant source files (inferred from stack trace: file paths + line numbers)
|
|
83
|
-
- Request context for runtime errors: path, method, payload keys (sanitized)
|
|
84
|
-
|
|
85
|
-
**Output:** Structured JSON passed to the agent.
|
|
86
|
-
|
|
87
|
-
**Location:** `utils/auto-fix/context-capture.js`
|
|
88
|
-
|
|
89
|
-
### 2. AI Agent
|
|
90
|
-
|
|
91
|
-
**Responsibilities:**
|
|
92
|
-
|
|
93
|
-
- Receive error context
|
|
94
|
-
- Call LLM with: error details, relevant file contents, project structure
|
|
95
|
-
- LLM returns: proposed fix (file path, old code, new code) or "cannot fix"
|
|
96
|
-
- Parse and validate the response
|
|
97
|
-
|
|
98
|
-
**LLM prompt structure:**
|
|
99
|
-
|
|
100
|
-
- System: "You are an expert debugger. Fix the error. Return only the minimal change."
|
|
101
|
-
- User: Error + stack + file contents + "What change would fix this?"
|
|
102
|
-
- Response format: Structured (JSON) with `{ file, oldSnippet, newSnippet }` or `{ reason: "cannot fix" }`
|
|
103
|
-
|
|
104
|
-
**Provider:** Pluggable (OpenAI, Anthropic). Config: `AUTO_FIX_LLM_PROVIDER`, `OPENAI_API_KEY` or `ANTHROPIC_API_KEY`.
|
|
105
|
-
|
|
106
|
-
**Location:** `utils/auto-fix/agent.js`
|
|
107
|
-
|
|
108
|
-
### 3. Fix Applicator
|
|
109
|
-
|
|
110
|
-
**Responsibilities:**
|
|
111
|
-
|
|
112
|
-
- Take proposed fix (file, oldSnippet, newSnippet)
|
|
113
|
-
- Validate: oldSnippet exists in file (fuzzy match if whitespace differs)
|
|
114
|
-
- Apply: replace oldSnippet with newSnippet
|
|
115
|
-
- Run tests again
|
|
116
|
-
- If tests pass → proceed to PR; if fail → retry with feedback or abort
|
|
117
|
-
|
|
118
|
-
**Location:** `utils/auto-fix/apply-fix.js`
|
|
119
|
-
|
|
120
|
-
### 4. GitHub PR Creator
|
|
121
|
-
|
|
122
|
-
**Responsibilities:**
|
|
123
|
-
|
|
124
|
-
- Create branch: `auto-fix/error-{hash}` or `auto-fix/{short-description}`
|
|
125
|
-
- Stage changed files, commit with message: "fix: [error summary]"
|
|
126
|
-
- Push to origin
|
|
127
|
-
- Open PR via GitHub API with: title, body (error context + fix summary), base branch
|
|
128
|
-
|
|
129
|
-
**Requirements:**
|
|
130
|
-
|
|
131
|
-
- `GITHUB_TOKEN` (PAT with `repo` scope) or `GH_TOKEN`
|
|
132
|
-
- Git remote must be configured (e.g. `origin` → GitHub)
|
|
133
|
-
- Run from a clean or committed working tree (or stash first)
|
|
134
|
-
|
|
135
|
-
**Location:** `utils/auto-fix/github-pr.js`
|
|
136
|
-
|
|
137
|
-
### 5. CLI Entry Point
|
|
138
|
-
|
|
139
|
-
**Command:** `tejas auto-fix` or `npm run test:fix`
|
|
140
|
-
|
|
141
|
-
**Flow:**
|
|
142
|
-
|
|
143
|
-
1. Run `vitest run` (or `vitest run --reporter=json` for structured output)
|
|
144
|
-
2. If exit code 0 → done
|
|
145
|
-
3. If exit code !== 0 → capture context from Vitest output
|
|
146
|
-
4. Invoke agent → apply fix → verify
|
|
147
|
-
5. If verified → create PR
|
|
148
|
-
6. Print PR URL or "Fix applied locally" (if no GitHub config)
|
|
149
|
-
|
|
150
|
-
**Location:** `cli/auto-fix.js` or `scripts/auto-fix.js`; add to `package.json` scripts
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
## File Structure
|
|
155
|
-
|
|
156
|
-
```
|
|
157
|
-
te.js/
|
|
158
|
-
├── utils/
|
|
159
|
-
│ └── auto-fix/
|
|
160
|
-
│ ├── index.js # Orchestrator
|
|
161
|
-
│ ├── should-trigger.js # Error filter (include/exclude/predicate)
|
|
162
|
-
│ ├── context-capture.js # Capture error + test output
|
|
163
|
-
│ ├── agent.js # LLM call, parse fix
|
|
164
|
-
│ ├── apply-fix.js # Apply patch, re-run tests
|
|
165
|
-
│ └── github-pr.js # Branch, commit, push, PR
|
|
166
|
-
├── cli/
|
|
167
|
-
│ └── auto-fix.js # CLI entry (or bin in package.json)
|
|
168
|
-
└── package.json # "test:fix": "node cli/auto-fix.js"
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
---
|
|
172
|
-
|
|
173
|
-
## Configuration
|
|
174
|
-
|
|
175
|
-
| Env / Config | Purpose |
|
|
176
|
-
| -------------------------------------- | ------------------------------------------------ | ----------- |
|
|
177
|
-
| `AUTO_FIX_ENABLED` | Enable feature (default: false) |
|
|
178
|
-
| `AUTO_FIX_LLM_PROVIDER` | `openai` | `anthropic` |
|
|
179
|
-
| `OPENAI_API_KEY` / `ANTHROPIC_API_KEY` | LLM API key |
|
|
180
|
-
| `GITHUB_TOKEN` / `GH_TOKEN` | GitHub PAT for PR creation |
|
|
181
|
-
| `AUTO_FIX_CREATE_PR` | If true, create PR; if false, only apply locally |
|
|
182
|
-
| `AUTO_FIX_BASE_BRANCH` | Branch to target (default: current or `main`) |
|
|
183
|
-
|
|
184
|
-
---
|
|
185
|
-
|
|
186
|
-
## Error Filtering: Developer Control
|
|
187
|
-
|
|
188
|
-
Developers must be able to control **which errors** trigger the auto-fix agent. Not every error should—e.g. 404s, auth failures, or intentional `TejError` throws may be undesirable.
|
|
189
|
-
|
|
190
|
-
### Filter Options
|
|
191
|
-
|
|
192
|
-
| Approach | Config | Use Case |
|
|
193
|
-
| -------------------- | --------------------------------- | --------------------------------------- |
|
|
194
|
-
| **Include only** | `autoFix.include` | Whitelist: only these trigger the agent |
|
|
195
|
-
| **Exclude** | `autoFix.exclude` | Blacklist: never trigger for these |
|
|
196
|
-
| **Custom predicate** | `autoFix.shouldFix(err, context)` | Full control via function |
|
|
197
|
-
|
|
198
|
-
### Filter Criteria (for include/exclude)
|
|
199
|
-
|
|
200
|
-
| Criterion | Example | Applies To |
|
|
201
|
-
| ------------------- | -------------------------------------- | ----------------- |
|
|
202
|
-
| **Error name** | `TypeError`, `TejError` | Both modes |
|
|
203
|
-
| **Status code** | `500`, `404` (exclude 404) | Runtime only |
|
|
204
|
-
| **Message pattern** | Regex: `/ECONNREFUSED/`, `/not found/` | Both |
|
|
205
|
-
| **File path** | `server/`**, `!tests/`** | Both (from stack) |
|
|
206
|
-
| **Route path** | `/api/`, `!/auth/login` | Runtime only |
|
|
207
|
-
| **Test file** | `**/*.test.js`, `!e2e/` | Test failure only |
|
|
208
|
-
|
|
209
|
-
### Configuration Examples
|
|
210
|
-
|
|
211
|
-
**tejas.config.json:**
|
|
212
|
-
|
|
213
|
-
```json
|
|
214
|
-
{
|
|
215
|
-
"autoFix": {
|
|
216
|
-
"enabled": true,
|
|
217
|
-
"include": {
|
|
218
|
-
"errorNames": ["TypeError", "ReferenceError"],
|
|
219
|
-
"statusCodes": [500],
|
|
220
|
-
"filePatterns": ["server/**", "utils/**"]
|
|
221
|
-
},
|
|
222
|
-
"exclude": {
|
|
223
|
-
"errorNames": ["TejError"],
|
|
224
|
-
"statusCodes": [404, 401, 403],
|
|
225
|
-
"messagePatterns": ["/ECONNREFUSED/", "/timeout/"],
|
|
226
|
-
"routePatterns": ["/auth/**", "/health"]
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
**Programmatic (custom predicate):**
|
|
233
|
-
|
|
234
|
-
```javascript
|
|
235
|
-
// index.js or tejas.config.js
|
|
236
|
-
const app = new Tejas({
|
|
237
|
-
autoFix: {
|
|
238
|
-
enabled: true,
|
|
239
|
-
shouldFix: (err, context) => {
|
|
240
|
-
// Never fix auth or 404
|
|
241
|
-
if (context.statusCode === 404 || context.statusCode === 401)
|
|
242
|
-
return false;
|
|
243
|
-
// Only fix errors in our source, not deps
|
|
244
|
-
if (!context.stackFiles?.some((f) => f.startsWith("server/")))
|
|
245
|
-
return false;
|
|
246
|
-
// Skip "connection refused" - infra issue, not code
|
|
247
|
-
if (/ECONNREFUSED|ETIMEDOUT/.test(err.message)) return false;
|
|
248
|
-
return true;
|
|
249
|
-
},
|
|
250
|
-
},
|
|
251
|
-
});
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
### Filter Evaluation Order
|
|
255
|
-
|
|
256
|
-
1. If `shouldFix` is provided → call it; if it returns `false`, **do not trigger**
|
|
257
|
-
2. If `include` is set → error must match at least one include criterion; else **do not trigger**
|
|
258
|
-
3. If `exclude` is set → error must match none of the exclude criteria; else **do not trigger**
|
|
259
|
-
4. Default (no config): **trigger** for all errors (when auto-fix is enabled)
|
|
260
|
-
|
|
261
|
-
### Context Passed to `shouldFix`
|
|
262
|
-
|
|
263
|
-
```javascript
|
|
264
|
-
{
|
|
265
|
-
error: Error, // The thrown error
|
|
266
|
-
errorName: string, // e.g. "TypeError"
|
|
267
|
-
message: string,
|
|
268
|
-
stack: string,
|
|
269
|
-
stackFiles: string[], // File paths from stack trace
|
|
270
|
-
statusCode?: number, // Runtime: TejError.code or 500
|
|
271
|
-
path?: string, // Runtime: request path
|
|
272
|
-
method?: string, // Runtime: GET, POST, etc.
|
|
273
|
-
testFile?: string, // Test failure: failing test file
|
|
274
|
-
testName?: string // Test failure: failing test name
|
|
275
|
-
}
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
### Location
|
|
279
|
-
|
|
280
|
-
- Filter logic: `utils/auto-fix/should-trigger.js`
|
|
281
|
-
- Called from: context capture (before invoking agent) and `errorHandler` (before enqueue)
|
|
282
|
-
- Config loading: reuse `loadConfigFile` + `standardizeObj` from [utils/configuration.js](utils/configuration.js)
|
|
283
|
-
|
|
284
|
-
---
|
|
285
|
-
|
|
286
|
-
## Safety and Guardrails
|
|
287
|
-
|
|
288
|
-
1. **Never run in production** — Only when `NODE_ENV=development` or explicit `AUTO_FIX_ENABLED=true`
|
|
289
|
-
2. **Sanitize context** — Redact secrets, tokens, PII from error payloads before sending to LLM
|
|
290
|
-
3. **Human review** — Fix goes to PR, never auto-merge
|
|
291
|
-
4. **Retry limit** — Agent tries at most N times (e.g. 2) before giving up
|
|
292
|
-
5. **Scope** — Only modify files inferred from stack trace; never touch `node_modules`, `.git`, config files (unless error points there)
|
|
293
|
-
|
|
294
|
-
---
|
|
295
|
-
|
|
296
|
-
## Runtime Error Integration (Optional, Phase 2)
|
|
297
|
-
|
|
298
|
-
To trigger from runtime errors in [server/handler.js](server/handler.js):
|
|
299
|
-
|
|
300
|
-
1. In `errorHandler`, after logging: if `env('AUTO_ERROR_FIX')` and `env('NODE_ENV') === 'development'`, call `shouldTrigger(err, context)`. If true, call `enqueueAutoFix(ammo, err)` (non-blocking)
|
|
301
|
-
2. `enqueueAutoFix` pushes to an in-memory queue or spawns a detached child process
|
|
302
|
-
3. Child process runs the same agent flow with runtime context (path, method, stack, sanitized payload)
|
|
303
|
-
4. Same apply → verify → PR flow
|
|
304
|
-
|
|
305
|
-
**Caveat:** Runtime errors may be non-deterministic (e.g. race, bad input). Agent might not reproduce the fix. Test failure mode is more reliable.
|
|
306
|
-
|
|
307
|
-
---
|
|
308
|
-
|
|
309
|
-
## Dependencies
|
|
310
|
-
|
|
311
|
-
- **Git operations:** `simple-git` or Node's `child_process` + `git` CLI
|
|
312
|
-
- **GitHub API:** `@octokit/rest` or `fetch` to `https://api.github.com`
|
|
313
|
-
- **LLM:** `openai` SDK or `@anthropic-ai/sdk` (or fetch to provider APIs)
|
|
314
|
-
- **Vitest:** Already present; use `vitest run --reporter=json` for parseable output
|
|
315
|
-
|
|
316
|
-
---
|
|
317
|
-
|
|
318
|
-
## Implementation Order
|
|
319
|
-
|
|
320
|
-
1. **Should-trigger filter** — Config loading, include/exclude/predicate logic (needed by both modes)
|
|
321
|
-
2. **Context capture** — Parse Vitest failure output, extract stack + files
|
|
322
|
-
3. **Agent** — LLM integration, prompt design, structured response parsing
|
|
323
|
-
4. **Apply fix** — Patch application, re-run tests
|
|
324
|
-
5. **GitHub PR** — Branch, commit, push, create PR
|
|
325
|
-
6. **CLI** — Wire it all together, `tejas auto-fix`
|
|
326
|
-
7. **Runtime trigger** (optional) — Enqueue from errorHandler
|
|
327
|
-
|
|
328
|
-
---
|
|
329
|
-
|
|
330
|
-
## Example Usage
|
|
331
|
-
|
|
332
|
-
```bash
|
|
333
|
-
# After a test failure
|
|
334
|
-
$ npm run test:fix
|
|
335
|
-
|
|
336
|
-
# Output:
|
|
337
|
-
# Tests failed. Running auto-fix agent...
|
|
338
|
-
# Agent proposed fix for server/ammo.js
|
|
339
|
-
# Re-running tests... passed.
|
|
340
|
-
# Created PR: https://github.com/user/te.js/pull/42
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
```javascript
|
|
344
|
-
// In tejas.config.json (for runtime mode, phase 2)
|
|
345
|
-
{
|
|
346
|
-
"autoFix": {
|
|
347
|
-
"enabled": true,
|
|
348
|
-
"createPr": true,
|
|
349
|
-
"exclude": {
|
|
350
|
-
"statusCodes": [404, 401, 403],
|
|
351
|
-
"routePatterns": ["/auth/**"]
|
|
352
|
-
}
|
|
353
|
-
},
|
|
354
|
-
"auto_fix_llm_provider": "openai"
|
|
355
|
-
}
|
|
356
|
-
```
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: Tejas Framework Test Suite
|
|
3
|
-
overview: Create a comprehensive test suite using Vitest for all core features of the Tejas framework, including unit tests, integration tests, edge cases, and error scenarios.
|
|
4
|
-
todos:
|
|
5
|
-
- id: setup-vitest
|
|
6
|
-
content: Set up Vitest configuration and install dependencies
|
|
7
|
-
status: completed
|
|
8
|
-
- id: test-helpers
|
|
9
|
-
content: Create test helper utilities and HTTP mocks
|
|
10
|
-
status: pending
|
|
11
|
-
- id: test-ammo
|
|
12
|
-
content: Write unit tests for Ammo class (fire, throw, redirect, shortcuts)
|
|
13
|
-
status: pending
|
|
14
|
-
- id: test-target-registry
|
|
15
|
-
content: Write tests for Target class and TargetRegistry route matching
|
|
16
|
-
status: pending
|
|
17
|
-
- id: test-body-parser
|
|
18
|
-
content: Write tests for body parser (JSON, URL-encoded, multipart)
|
|
19
|
-
status: pending
|
|
20
|
-
- id: test-rate-limiting
|
|
21
|
-
content: Write tests for all 3 rate limiting algorithms
|
|
22
|
-
status: pending
|
|
23
|
-
- id: test-file-uploader
|
|
24
|
-
content: Write tests for file upload middleware
|
|
25
|
-
status: pending
|
|
26
|
-
- id: test-configuration
|
|
27
|
-
content: Write tests for configuration loading and standardization
|
|
28
|
-
status: pending
|
|
29
|
-
- id: test-handler-integration
|
|
30
|
-
content: Write integration tests for request handler and middleware chain
|
|
31
|
-
status: pending
|
|
32
|
-
---
|
|
33
|
-
|
|
34
|
-
# Tejas Framework Test Suite Plan
|
|
35
|
-
|
|
36
|
-
Create comprehensive tests using Vitest for all core features of the Tejas framework, organized by module with unit, integration, and error scenario coverage.
|
|
37
|
-
|
|
38
|
-
## Test Infrastructure Setup
|
|
39
|
-
|
|
40
|
-
Install Vitest and configure for ES modules. Create test utilities and mocks for HTTP requests, database connections, and file system operations.**Files to create:**
|
|
41
|
-
|
|
42
|
-
- `vitest.config.js` - Vitest configuration with ESM support
|
|
43
|
-
- `tests/helpers/mock-http.js` - HTTP request/response mocks
|
|
44
|
-
- `tests/helpers/test-utils.js` - Common test utilities
|
|
45
|
-
|
|
46
|
-
## Core Module Tests
|
|
47
|
-
|
|
48
|
-
### 1. Ammo Class ([server/ammo.js](server/ammo.js))
|
|
49
|
-
|
|
50
|
-
Unit tests for the request/response handler:
|
|
51
|
-
|
|
52
|
-
- HTTP method flag initialization (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
|
|
53
|
-
- `fire()` response dispatch with various argument patterns (status only, data only, status+data, status+data+content-type)
|
|
54
|
-
- `redirect()` with default and custom status codes
|
|
55
|
-
- `throw()` error handling (TejError, Error instances, status codes, strings)
|
|
56
|
-
- `notFound()`, `notAllowed()`, `unauthorized()` shortcuts
|
|
57
|
-
- `enhance()` method populating request properties
|
|
58
|
-
|
|
59
|
-
### 2. Target & Registry ([server/target.js](server/target.js), [server/targets/registry.js](server/targets/registry.js))
|
|
60
|
-
|
|
61
|
-
Unit tests for routing:
|
|
62
|
-
|
|
63
|
-
- Target constructor with base path
|
|
64
|
-
- `midair()` middleware registration
|
|
65
|
-
- `register()` endpoint registration with path + handler + optional middlewares
|
|
66
|
-
- TargetRegistry singleton pattern
|
|
67
|
-
- `aim()` exact path matching
|
|
68
|
-
- `aim()` parameterized route matching (`/users/:id`, `/api/:category/:id`)
|
|
69
|
-
- `getAllEndpoints()` listing (flat and grouped)
|
|
70
|
-
|
|
71
|
-
### 3. Request Handler ([server/handler.js](server/handler.js))
|
|
72
|
-
|
|
73
|
-
Integration tests for request processing:
|
|
74
|
-
|
|
75
|
-
- Middleware chain execution order
|
|
76
|
-
- Global + target + route middleware composition
|
|
77
|
-
- Async middleware handling
|
|
78
|
-
- Error propagation through chain
|
|
79
|
-
- 404 handling for unmatched routes
|
|
80
|
-
- Response already sent detection
|
|
81
|
-
|
|
82
|
-
### 4. Body Parser ([server/ammo/body-parser.js](server/ammo/body-parser.js))
|
|
83
|
-
|
|
84
|
-
Unit tests for request body parsing:
|
|
85
|
-
|
|
86
|
-
- JSON body parsing (valid, invalid, empty)
|
|
87
|
-
- URL-encoded body parsing
|
|
88
|
-
- Multipart form data parsing with boundary extraction
|
|
89
|
-
- Body size limit enforcement (413 errors)
|
|
90
|
-
- Request timeout handling (408 errors)
|
|
91
|
-
- Missing content-type handling
|
|
92
|
-
|
|
93
|
-
### 5. Rate Limiting ([rate-limit/](rate-limit/))
|
|
94
|
-
|
|
95
|
-
Unit tests for each algorithm:
|
|
96
|
-
|
|
97
|
-
- **Fixed Window:** Counter increments, window reset, strict mode alignment
|
|
98
|
-
- **Sliding Window:** Weighted counts, timestamp pruning, window transitions
|
|
99
|
-
- **Token Bucket:** Token consumption, refill rate, burst handling
|
|
100
|
-
- Storage interface (memory operations)
|
|
101
|
-
- Rate limit header generation (standard, legacy, draft7, draft8)
|
|
102
|
-
- `onRateLimited` callback
|
|
103
|
-
|
|
104
|
-
### 6. File Uploader ([server/files/uploader.js](server/files/uploader.js))
|
|
105
|
-
|
|
106
|
-
Unit tests for file handling:
|
|
107
|
-
|
|
108
|
-
- Single file upload with `file()` middleware
|
|
109
|
-
- Multiple file upload with `files()` middleware
|
|
110
|
-
- File size validation (413 errors for oversized files)
|
|
111
|
-
- File metadata extraction (extension, mimetype, path)
|
|
112
|
-
- Non-multipart request passthrough
|
|
113
|
-
- Directory creation
|
|
114
|
-
|
|
115
|
-
### 7. Configuration ([utils/configuration.js](utils/configuration.js))
|
|
116
|
-
|
|
117
|
-
Unit tests for config processing:
|
|
118
|
-
|
|
119
|
-
- `loadConfigFile()` file reading and JSON parsing
|
|
120
|
-
- `standardizeObj()` key uppercasing and flattening
|
|
121
|
-
- Missing config file handling
|
|
122
|
-
- Nested object flattening with underscores
|
|
123
|
-
|
|
124
|
-
### 8. TejError ([server/error.js](server/error.js))
|
|
125
|
-
|
|
126
|
-
Unit tests for custom error:
|
|
127
|
-
|
|
128
|
-
- Constructor with code and message
|
|
129
|
-
- Error inheritance
|
|
130
|
-
- Name property
|
|
131
|
-
|
|
132
|
-
### 9. Database Manager ([database/index.js](database/index.js))
|
|
133
|
-
|
|
134
|
-
Unit tests (mocked connections):
|
|
135
|
-
|
|
136
|
-
- Singleton pattern enforcement
|
|
137
|
-
- Connection initialization tracking
|
|
138
|
-
- `hasConnection()` status checking
|
|
139
|
-
- `getConnection()` retrieval
|
|
140
|
-
- Connection not found error
|
|
141
|
-
|
|
142
|
-
## Test File Structure
|
|
143
|
-
|
|
144
|
-
```javascript
|
|
145
|
-
tests/
|
|
146
|
-
helpers/
|
|
147
|
-
mock-http.js
|
|
148
|
-
test-utils.js
|
|
149
|
-
unit/
|
|
150
|
-
ammo.test.js
|
|
151
|
-
target.test.js
|
|
152
|
-
registry.test.js
|
|
153
|
-
body-parser.test.js
|
|
154
|
-
rate-limit/
|
|
155
|
-
fixed-window.test.js
|
|
156
|
-
sliding-window.test.js
|
|
157
|
-
token-bucket.test.js
|
|
158
|
-
file-uploader.test.js
|
|
159
|
-
configuration.test.js
|
|
160
|
-
tej-error.test.js
|
|
161
|
-
database-manager.test.js
|
|
162
|
-
integration/
|
|
163
|
-
handler.test.js
|
|
164
|
-
middleware-chain.test.js
|
|
165
|
-
rate-limit-middleware.test.js
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
```
|
package/.prettierignore
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# Dependencies
|
|
2
|
-
node_modules/
|
|
3
|
-
package-lock.json
|
|
4
|
-
yarn.lock
|
|
5
|
-
|
|
6
|
-
# Build output
|
|
7
|
-
dist/
|
|
8
|
-
build/
|
|
9
|
-
*.min.js
|
|
10
|
-
|
|
11
|
-
# Coverage directory
|
|
12
|
-
coverage/
|
|
13
|
-
|
|
14
|
-
# Cache directories
|
|
15
|
-
.cache/
|
|
16
|
-
.npm/
|
|
17
|
-
|
|
18
|
-
# Logs
|
|
19
|
-
logs/
|
|
20
|
-
*.log
|
|
21
|
-
npm-debug.log*
|
|
22
|
-
|
|
23
|
-
# IDE specific files
|
|
24
|
-
.idea/
|
|
25
|
-
.vscode/
|
|
26
|
-
*.swp
|
|
27
|
-
*.swo
|
|
28
|
-
|
|
29
|
-
# System Files
|
|
30
|
-
.DS_Store
|
|
31
|
-
Thumbs.db
|
package/.prettierrc
DELETED
package/example/API_OVERVIEW.md
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
# API Documentation
|
|
2
|
-
|
|
3
|
-
**Version:** 1.0.0
|
|
4
|
-
|
|
5
|
-
## Project Summary
|
|
6
|
-
|
|
7
|
-
This API provides a lightweight service combining user account management, a Redis-backed caching layer, and system introspection utilities. It supports full CRUD operations on user resources—including file uploads for profile images and bulk documents—offers key-value caching with optional TTL, and exposes health-check and route discovery endpoints for operational monitoring and API introspection. User data is maintained in-memory for demonstration purposes, and uploaded files are stored on disk with configurable size limits.
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## APIs Available
|
|
12
|
-
|
|
13
|
-
### Users
|
|
14
|
-
Manages user accounts with full CRUD operations: create, retrieve, update, and delete users. Additionally supports profile image uploads and bulk document uploads, with files stored on disk and configurable size limits.
|
|
15
|
-
|
|
16
|
-
### Cache
|
|
17
|
-
Provides a key-value caching layer backed by Redis. Store arbitrary values with an optional time-to-live (TTL) and retrieve them by key. A middleware guard ensures the Redis service is available before processing any cache request.
|
|
18
|
-
|
|
19
|
-
### System & Discovery
|
|
20
|
-
Core application utilities including a default landing endpoint, a health-check endpoint reporting server status with a current timestamp, and a route discovery endpoint that lists all registered endpoints grouped by source for API introspection.
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## Key Endpoints
|
|
25
|
-
|
|
26
|
-
### Getting Started
|
|
27
|
-
|
|
28
|
-
| Method | Path | Description |
|
|
29
|
-
|--------|------|-------------|
|
|
30
|
-
| `GET` | `/` | Default landing entry point for the API. |
|
|
31
|
-
| `GET` | `/health` | Health check — returns server status and current timestamp. |
|
|
32
|
-
| `GET` | `/routes` | Route discovery — lists all registered endpoints grouped by source. |
|
|
33
|
-
|
|
34
|
-
### User Management
|
|
35
|
-
|
|
36
|
-
| Method | Path | Description |
|
|
37
|
-
|--------|------|-------------|
|
|
38
|
-
| `GET` | `/users` | Retrieve a list of all users. |
|
|
39
|
-
| `POST` | `/users` | Create a new user. |
|
|
40
|
-
| `GET` | `/users/{id}` | Retrieve a specific user by ID. |
|
|
41
|
-
| `PUT` | `/users/{id}` | Update an existing user by ID. |
|
|
42
|
-
| `DELETE` | `/users/{id}` | Delete a user by ID. |
|
|
43
|
-
| `GET` | `/users/{id}/updateProfileImage` | Upload or update a user's profile image. |
|
|
44
|
-
| `GET` | `/users/{id}/uploadDocuments` | Bulk upload documents for a user. |
|
|
45
|
-
|
|
46
|
-
### Cache Operations
|
|
47
|
-
|
|
48
|
-
| Method | Path | Description |
|
|
49
|
-
|--------|------|-------------|
|
|
50
|
-
| `POST` | `/cache` | Store a value in the cache with an optional TTL. |
|
|
51
|
-
| `GET` | `/cache/{key}` | Retrieve a cached value by its key. |
|
|
52
|
-
|
|
53
|
-
---
|
|
54
|
-
|
|
55
|
-
## Additional Notes
|
|
56
|
-
|
|
57
|
-
### Data Storage
|
|
58
|
-
- **User data** is maintained in-memory and is not persisted across server restarts (demonstration mode).
|
|
59
|
-
- **Uploaded files** (profile images, documents) are stored on disk with configurable size limits.
|
|
60
|
-
|
|
61
|
-
### Cache Availability
|
|
62
|
-
All cache endpoints are protected by a middleware guard that verifies the Redis service is reachable before processing requests. If Redis is unavailable, cache requests will be rejected before reaching the handler.
|
|
63
|
-
|
|
64
|
-
### File Uploads
|
|
65
|
-
- **Profile images**: Single-file upload tied to a specific user via `/users/{id}/updateProfileImage`.
|
|
66
|
-
- **Bulk documents**: Multi-file upload supported via `/users/{id}/uploadDocuments`.
|
|
67
|
-
|
|
68
|
-
File size limits are configurable at the application level.
|
|
69
|
-
|
|
70
|
-
---
|
|
71
|
-
|
|
72
|
-
## Quick Start
|
|
73
|
-
|
|
74
|
-
1. **Verify the API is running** — call `GET /health` and confirm a successful status response with a timestamp.
|
|
75
|
-
2. **Explore available routes** — call `GET /routes` to discover all registered endpoints.
|
|
76
|
-
3. **Create a user** — send a `POST` request to `/users` with the required user payload.
|
|
77
|
-
4. **Store and retrieve cache entries** — use `POST /cache` to set a key-value pair, then `GET /cache/{key}` to retrieve it.
|