feat(KB-180): add planning route diagnostics and harden JSON error handling
- Add API diagnostics to planning routes for better debugging - Harden API JSON error handling with improved validation - Add comprehensive tests for planning route JSON responses - Update server routes and API layer for robustness
This commit is contained in:
@@ -565,6 +565,18 @@ function pushGitBranch(): GitPushResult {
|
||||
|
||||
export function createApiRoutes(store: TaskStore, options?: ServerOptions): Router {
|
||||
const router = Router();
|
||||
|
||||
if (process.env.KB_DEBUG_PLANNING_ROUTES === "1") {
|
||||
const planningRoutes = [
|
||||
"POST /planning/start",
|
||||
"POST /planning/start-streaming",
|
||||
"POST /planning/respond",
|
||||
"POST /planning/cancel",
|
||||
"POST /planning/create-task",
|
||||
"GET /planning/:sessionId/stream",
|
||||
];
|
||||
console.debug("[planning:routes:registered]", planningRoutes);
|
||||
}
|
||||
const sessionFilesCache = new Map<string, { files: string[]; expiresAt: number }>();
|
||||
|
||||
// Get GitHub token from options or env
|
||||
|
||||
@@ -50,6 +50,38 @@ async function GET(app: express.Express, path: string): Promise<{ status: number
|
||||
});
|
||||
}
|
||||
|
||||
async function REQUEST(
|
||||
app: express.Express,
|
||||
method: string,
|
||||
path: string,
|
||||
body?: string,
|
||||
headers?: Record<string, string>,
|
||||
): Promise<{ status: number; body: unknown; headers: http.IncomingHttpHeaders }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = app.listen(0, () => {
|
||||
const addr = server.address() as { port: number };
|
||||
const req = http.request(
|
||||
{ hostname: "127.0.0.1", port: addr.port, path, method, headers },
|
||||
(res) => {
|
||||
let data = "";
|
||||
res.on("data", (chunk) => (data += chunk));
|
||||
res.on("end", () => {
|
||||
server.close();
|
||||
try {
|
||||
resolve({ status: res.statusCode!, body: JSON.parse(data), headers: res.headers });
|
||||
} catch {
|
||||
resolve({ status: res.statusCode!, body: data, headers: res.headers });
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on("error", (err) => { server.close(); reject(err); });
|
||||
if (body) req.write(body);
|
||||
req.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("API Error Handling Middleware", () => {
|
||||
let store: TaskStore;
|
||||
|
||||
@@ -110,4 +142,40 @@ describe("API Error Handling Middleware", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("planning API route content types", () => {
|
||||
it("returns JSON for all POST planning endpoints instead of falling through to SPA HTML", async () => {
|
||||
const endpoints = [
|
||||
"/api/planning/start",
|
||||
"/api/planning/start-streaming",
|
||||
"/api/planning/respond",
|
||||
"/api/planning/cancel",
|
||||
"/api/planning/create-task",
|
||||
];
|
||||
|
||||
for (const path of endpoints) {
|
||||
const app = createServer(store);
|
||||
const res = await REQUEST(app, "POST", path, JSON.stringify({}), {
|
||||
"Content-Type": "application/json",
|
||||
});
|
||||
|
||||
expect(res.headers["content-type"]).toContain("application/json");
|
||||
if (typeof res.body === "string") {
|
||||
expect(res.body).not.toContain("<!DOCTYPE html>");
|
||||
expect(res.body).not.toContain("<html");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("returns JSON 404s for unmatched planning API routes", async () => {
|
||||
const app = createServer(store);
|
||||
const res = await REQUEST(app, "POST", "/api/planning/not-a-route", JSON.stringify({}), {
|
||||
"Content-Type": "application/json",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.headers["content-type"]).toContain("application/json");
|
||||
expect(res.body).toEqual({ error: "Not found" });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -166,6 +166,19 @@ export function createServer(store: TaskStore, options?: ServerOptions): ReturnT
|
||||
// Rate limiting — mutation endpoints (POST/PUT/PATCH/DELETE)
|
||||
app.use("/api", rateLimit(RATE_LIMITS.api));
|
||||
|
||||
// Planning route diagnostics for production/runtime debugging. Disabled by default.
|
||||
if (process.env.KB_DEBUG_PLANNING_ROUTES === "1") {
|
||||
app.use("/api/planning", (req, _res, next) => {
|
||||
console.debug("[planning:request]", {
|
||||
method: req.method,
|
||||
path: req.path,
|
||||
originalUrl: req.originalUrl,
|
||||
contentType: req.headers["content-type"],
|
||||
});
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
// REST API
|
||||
app.use("/api", createApiRoutes(store, options));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user