feat(auth): harden session management and security

- Implement server-side session revocation on logout
- Add IP extraction utility and admin setup error handling
- Improve error codes and response consistency for auth endpoints
- Refine landing page redirect logic to handle transient DB outages
- Update deployment documentation and configuration defaults
This commit is contained in:
Team Dark Devil committed 2026-11-19 20:22:01 +06:00
1 parent 935f12a1e9
commit 89e4d68690
81 files changed
+11530 -6916

No files matched your search

+695 -14
View File
@@ -1,29 +1,45 @@
import assert from "node:assert/strict";
import { after, afterEach, beforeEach, describe, it } from "node:test";
import { NextRequest } from "next/server";
import { POST as loginPost } from "@/app/api/auth/login/route";
import { POST as logoutPost } from "@/app/api/auth/logout/route";
import { GET as profileGet, PUT as profilePut } from "@/app/api/auth/profile/route";
import { POST as setupPost } from "@/app/api/auth/setup/route";
import { GET as clientsGet, POST as clientsPost } from "@/app/api/clients/route";
import {
DELETE as clientDelete,
GET as clientGet,
PATCH as clientPatch,
} from "@/app/api/clients/[id]/route";
import { POST as eventsPost } from "@/app/api/events/route";
import { GET as dataGet } from "@/app/api/data/route";
import { GET as eventsGet, POST as eventsPost } from "@/app/api/events/route";
import { GET as exportGet } from "@/app/api/export/route";
import { GET as healthGet } from "@/app/api/health/route";
import { GET as metricsGet } from "@/app/api/metrics/route";
import { GET as storageGet, POST as storagePost } from "@/app/api/storage/route";
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import {
createAdminSessionToken,
getAdminSessionPayload,
getAdminSessionSecret,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
AdminDatabaseRequiredError,
consumeAdminLoginAttempt,
createInitialAdmin,
getAdminSetupKey,
isAdminSetupComplete,
requiresPersistentAdminDatabase,
resetAdminStateForTests,
verifyAdminCredentials,
verifyAdminSetupKey,
verifyRequestAdminSession,
} from "@/core/auth/adminUsers";
import { AuthService, resolveDefaultJwtSecret } from "@/core/auth/jwt";
import { DatabaseEngine } from "@/core/Database/database";
import { SDKManager } from "@/core/sdk/sdkManager";
import { extractClientIp } from "@/lib/adminAuth";
import { isSameOriginRequest, middleware } from "@/middleware";
const ORIGINAL_ENV = { ...process.env };
@@ -41,7 +57,14 @@ function restoreEnv() {
describe("Phase-01 Auth, Secrets, Revocation & Proxy Hardening", () => {
beforeEach(() => {
restoreEnv();
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
resetAdminStateForTests();
const g = globalThis as {
__dataStackClientsMap?: Map<string, unknown>;
__dataStackDeletedClients?: Set<string>;
};
g.__dataStackClientsMap?.clear();
g.__dataStackDeletedClients?.clear();
});
afterEach(() => {
@@ -338,22 +361,18 @@ describe("Phase-01 Auth, Secrets, Revocation & Proxy Hardening", () => {
assert.equal(isSameOriginRequest(reqMissingBoth), false);
});
it("SPEC-08: allows headless scripts with x-datastack-admin-client or DataStack-CLI to bypass browser CSRF check", async () => {
it("BUG-A02: rejects spoofable x-datastack-admin-client, DataStack-CLI, and cross-site Sec-Fetch-Site headers without same-origin proof", async () => {
process.env.DATASTACK_ADMIN_SESSION_SECRET = "test-secret-at-least-32-chars-long-for-session";
// 1. Without headless header or origin -> 403 CSRF rejected
// 1. Missing origin/referer -> 403
const blockedReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
host: "localhost:3000",
},
headers: { host: "localhost:3000" },
});
const blockedRes = await middleware(blockedReq);
assert.equal(blockedRes.status, 403);
const blockedJson = await blockedRes.json();
assert.match(blockedJson.error, /cross-origin/i);
// 2. With x-datastack-admin-client header -> bypasses CSRF (reaches auth check, returns 401 Unauthorized if no cookie)
// 2. Spoofed x-datastack-admin-client header must still be rejected 403
const headlessReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
@@ -362,19 +381,681 @@ describe("Phase-01 Auth, Secrets, Revocation & Proxy Hardening", () => {
},
});
const headlessRes = await middleware(headlessReq);
assert.equal(headlessRes.status, 401);
const headlessJson = await headlessRes.json();
assert.match(headlessJson.error, /authentication required/i);
assert.equal(headlessRes.status, 403);
// 3. With DataStack-CLI User-Agent -> bypasses CSRF
// 3. Spoofed DataStack-CLI User-Agent + cross-origin must still be rejected 403
const cliReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "https://evil.example.com",
"user-agent": "DataStack-CLI/1.0",
},
});
const cliRes = await middleware(cliReq);
assert.equal(cliRes.status, 401);
assert.equal(cliRes.status, 403);
// 4. Genuine same-origin passes CSRF and reaches auth check (401 without cookie)
const sameOriginReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "http://localhost:3000",
},
});
const sameOriginRes = await middleware(sameOriginReq);
assert.equal(sameOriginRes.status, 401);
});
it("BUG-A01: enforces tokenVersion revocation across /api/data, GET /api/events, /api/export, /api/metrics, and /api/storage", async () => {
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
await createInitialAdmin("owner@datastack.dev", "InitialPass123!");
const secret = getAdminSessionSecret()!;
const v1Token = await createAdminSessionToken(
"owner@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
const updateRes = await profilePut(
new NextRequest("http://localhost:3000/api/auth/profile", {
method: "PUT",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${v1Token}`,
},
body: JSON.stringify({
email: "owner@datastack.dev",
currentPassword: "InitialPass123!",
newPassword: "RotatedPass456!",
}),
}),
);
assert.equal(updateRes.status, 200);
const v2Cookie = updateRes.cookies.get("datastack_admin_session")?.value;
assert.ok(v2Cookie);
const endpoints: Array<() => Promise<Response>> = [
() => dataGet(new NextRequest("http://localhost:3000/api/data", { headers: { cookie: `datastack_admin_session=${v1Token}` } })),
() => eventsGet(new NextRequest("http://localhost:3000/api/events", { headers: { cookie: `datastack_admin_session=${v1Token}` } })),
() => exportGet(new NextRequest("http://localhost:3000/api/export?flush=false", { headers: { cookie: `datastack_admin_session=${v1Token}` } })),
() => metricsGet(new NextRequest("http://localhost:3000/api/metrics", { headers: { cookie: `datastack_admin_session=${v1Token}` } })),
() => storageGet(new NextRequest("http://localhost:3000/api/storage", { headers: { cookie: `datastack_admin_session=${v1Token}` } })),
() => storagePost(new NextRequest("http://localhost:3000/api/storage", { method: "POST", headers: { cookie: `datastack_admin_session=${v1Token}` } })),
];
for (const invoke of endpoints) {
const res = await invoke();
assert.equal(res.status, 401);
}
const okMetrics = await metricsGet(
new NextRequest("http://localhost:3000/api/metrics", {
headers: { cookie: `datastack_admin_session=${v2Cookie}` },
}),
);
assert.equal(okMetrics.status, 200);
});
it("BUG-A04 & BUG-A05: rejects unregistered clients on /api/health & admin POST /api/events and validates PATCH /api/clients/[id]", async () => {
delete process.env.DATABASE_URL;
await createInitialAdmin("admin@datastack.dev", "AdminPass123!");
const secret = getAdminSessionSecret()!;
const adminCookie = await createAdminSessionToken(
"admin@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
// Public unauthenticated /api/health probe stays 200
const publicHealth = await healthGet(new NextRequest("http://localhost:3000/api/health"));
assert.equal(publicHealth.status, 200);
// Unregistered signed client Bearer token on /api/health -> 403 CLIENT_REVOKED
const ghostToken = AuthService.generateClientToken({ clientId: "ghost_health_client" });
const ghostHealth = await healthGet(
new NextRequest("http://localhost:3000/api/health", {
headers: { authorization: `Bearer ${ghostToken}` },
}),
);
assert.equal(ghostHealth.status, 403);
// Admin cookie POST /api/events for unregistered clientId -> 403 CLIENT_REVOKED
const ghostAdminPost = await eventsPost(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${adminCookie}`,
},
body: JSON.stringify({ clientId: "unregistered_admin_target", data: { ok: 1 } }),
}),
);
assert.equal(ghostAdminPost.status, 403);
// Create normalized client and verify mixed-case/unvalidated PATCH checks
const createdRes = await clientsPost(
new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${adminCookie}`,
},
body: JSON.stringify({ clientId: "Norm_Client_01", name: "Normalized", rateLimit: 50 }),
}),
);
assert.equal(createdRes.status, 201);
// Invalid non-string name -> 400 (not 500)
const badNameRes = await clientPatch(
new NextRequest("http://localhost:3000/api/clients/Norm_Client_01", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${adminCookie}`,
},
body: JSON.stringify({ name: 12345 }),
}),
{ params: Promise.resolve({ id: "Norm_Client_01" }) },
);
assert.equal(badNameRes.status, 400);
// Invalid status -> 400
const badStatusRes = await clientPatch(
new NextRequest("http://localhost:3000/api/clients/Norm_Client_01", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${adminCookie}`,
},
body: JSON.stringify({ status: "suspended" }),
}),
{ params: Promise.resolve({ id: "Norm_Client_01" }) },
);
assert.equal(badStatusRes.status, 400);
// Valid mixed-case normalized PATCH -> 200
const goodPatchRes = await clientPatch(
new NextRequest("http://localhost:3000/api/clients/NORM_CLIENT_01", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: `datastack_admin_session=${adminCookie}`,
},
body: JSON.stringify({ name: "Updated Clean Name", rateLimit: 250 }),
}),
{ params: Promise.resolve({ id: "NORM_CLIENT_01" }) },
);
assert.equal(goodPatchRes.status, 200);
const patchedJson = await goodPatchRes.json();
assert.equal(patchedJson.client.clientId, "norm_client_01");
assert.equal(patchedJson.client.rateLimit, 250);
});
it("FORENSIC BUG-01: blocks volatile RAM admin setup in production without DATABASE_URL and returns 409 SETUP_REQUIRED on pre-setup login", async () => {
process.env.NODE_ENV = "production";
delete process.env.DATABASE_URL;
delete process.env.DATASTACK_ADMIN_PASSWORD;
process.env.DATASTACK_ADMIN_SESSION_SECRET =
"prod-session-secret-at-least-32-characters-long";
process.env.DATASTACK_ADMIN_SETUP_KEY =
"prod-setup-key-at-least-32-characters-long";
assert.equal(requiresPersistentAdminDatabase(), true);
await assert.rejects(
() => createInitialAdmin("owner@datastack.dev", "StrongPass123!"),
AdminDatabaseRequiredError,
);
const setupRes = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
host: "localhost:3000",
origin: "http://localhost:3000",
},
body: JSON.stringify({
email: "owner@datastack.dev",
password: "StrongPass123!",
setupKey: process.env.DATASTACK_ADMIN_SETUP_KEY,
}),
}),
);
assert.equal(setupRes.status, 503);
const setupJson = await setupRes.json();
assert.equal(setupJson.code, "DATABASE_NOT_CONFIGURED");
const loginRes = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
"content-type": "application/json",
host: "localhost:3000",
origin: "http://localhost:3000",
},
body: JSON.stringify({
email: "owner@datastack.dev",
password: "StrongPass123!",
}),
}),
);
assert.equal(loginRes.status, 409);
const loginJson = await loginRes.json();
assert.equal(loginJson.code, "SETUP_REQUIRED");
});
it("FORENSIC BUG-04: throttles POST /api/auth/setup brute-force attempts and checks setup completion before verifying setupKey", async () => {
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
process.env.DATASTACK_ADMIN_SETUP_KEY =
"custom-setup-key-at-least-32-characters-long";
for (let i = 0; i < 5; i++) {
const badRes = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "198.51.100.77",
},
body: JSON.stringify({
email: "admin@datastack.dev",
password: "StrongPass123!",
setupKey: "wrong-key-attempt",
}),
}),
);
assert.equal(badRes.status, 403);
}
const throttledRes = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "198.51.100.77",
},
body: JSON.stringify({
email: "admin@datastack.dev",
password: "StrongPass123!",
setupKey: process.env.DATASTACK_ADMIN_SETUP_KEY,
}),
}),
);
assert.equal(throttledRes.status, 429);
assert.ok(Number(throttledRes.headers.get("retry-after")) >= 1);
// Different IP succeeds and initializes admin
const okRes = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "203.0.113.88",
},
body: JSON.stringify({
email: "admin@datastack.dev",
password: "StrongPass123!",
setupKey: process.env.DATASTACK_ADMIN_SETUP_KEY,
}),
}),
);
assert.equal(okRes.status, 200);
// Subsequent setup attempt returns 409 even if setupKey is invalid
const postInitRes = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "203.0.113.89",
},
body: JSON.stringify({
email: "other@datastack.dev",
password: "StrongPass123!",
setupKey: "completely-invalid-key",
}),
}),
);
assert.equal(postInitRes.status, 409);
});
it("FORENSIC BUG-05: revokes session cookie server-side on logout, ignores spoofed Origin for Secure flag, and allows privacy-stripped same-origin requests", async () => {
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
await createInitialAdmin("logout-test@datastack.dev", "LogoutPass123!");
const secret = getAdminSessionSecret()!;
const sessionToken = await createAdminSessionToken(
"logout-test@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
// Valid before logout
const preLogoutRes = await profileGet(
new NextRequest("http://localhost:3000/api/auth/profile", {
headers: { cookie: `datastack_admin_session=${sessionToken}` },
}),
);
assert.equal(preLogoutRes.status, 200);
// Logout bumps tokenVersion server-side
const logoutRes = await logoutPost(
new NextRequest("http://localhost:3000/api/auth/logout", {
method: "POST",
headers: { cookie: `datastack_admin_session=${sessionToken}` },
}),
);
assert.equal(logoutRes.status, 200);
// Replaying captured pre-logout cookie must return 401
const replayProfile = await profileGet(
new NextRequest("http://localhost:3000/api/auth/profile", {
headers: { cookie: `datastack_admin_session=${sessionToken}` },
}),
);
assert.equal(replayProfile.status, 401);
// Cookie Secure flag must ignore spoofed https Origin on plain http request
const spoofedOriginReq = new Request("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { origin: "https://evil.example.com" },
});
const plainOpts = getSessionCookieOptions(spoofedOriginReq, 3600);
assert.equal(plainOpts.secure, false);
assert.equal(plainOpts.sameSite, "lax");
const httpsProxyReq = new Request("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { "x-forwarded-proto": "https" },
});
const httpsOpts = getSessionCookieOptions(httpsProxyReq, 3600);
assert.equal(httpsOpts.secure, true);
assert.equal(httpsOpts.sameSite, "none");
// Privacy extension stripping Origin & Referer with Sec-Fetch-Site: same-origin passes CSRF
const privacySameOriginReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
host: "localhost:3000",
"sec-fetch-site": "same-origin",
},
});
assert.equal(isSameOriginRequest(privacySameOriginReq), true);
const crossSiteReq = new NextRequest("http://localhost:3000/api/clients", {
method: "POST",
headers: {
host: "localhost:3000",
"sec-fetch-site": "cross-site",
},
});
assert.equal(isSameOriginRequest(crossSiteReq), false);
});
it("FORENSIC BUG-06: prevents empty production jwtSecret PG persistence and propagates DB write errors instead of silently swallowing them", async () => {
const origGetPool = DatabaseEngine.getPool;
const origEnsureSchema = DatabaseEngine.ensureSchema;
const settingsGlobals = globalThis as {
__dataStackAdminSettings?: unknown;
__dataStackSettingsHydrated?: boolean;
};
const queries: string[] = [];
try {
// 1. Production with empty jwtSecret must not persist empty jwt_secret row into PG
process.env.NODE_ENV = "production";
delete process.env.DATASTACK_JWT_SECRET;
settingsGlobals.__dataStackAdminSettings = undefined;
settingsGlobals.__dataStackSettingsHydrated = false;
DatabaseEngine.getPool = (() =>
({
query: async (sql: string) => {
queries.push(sql);
return { rowCount: 0, rows: [] };
},
}) as unknown as ReturnType<typeof DatabaseEngine.getPool>);
DatabaseEngine.ensureSchema = async () => true;
await SettingsManager.getSettingsAsync();
assert.equal(
queries.some((sql) => sql.includes("INSERT INTO admin_settings")),
false,
);
// 2. Failing PG write must reject (not silently return 200) and must not mutate RAM
process.env.NODE_ENV = "test";
process.env.DATASTACK_JWT_SECRET =
"valid-jwt-secret-at-least-32-characters-long";
AuthService.setSecret(process.env.DATASTACK_JWT_SECRET);
settingsGlobals.__dataStackAdminSettings = undefined;
settingsGlobals.__dataStackSettingsHydrated = false;
DatabaseEngine.getPool = (() =>
({
query: async (sql: string) => {
if (sql.includes("INSERT INTO") || sql.includes("UPDATE")) {
throw new Error("simulated PG outage");
}
return { rowCount: 0, rows: [] };
},
}) as unknown as ReturnType<typeof DatabaseEngine.getPool>);
await assert.rejects(
() => SettingsManager.updateSettingsAsync({ maxEventsPerSec: 777 }),
/DATABASE_WRITE_FAILED/,
);
await assert.rejects(
() =>
SDKManager.registerClientAsync({
clientId: "db_fail_client",
name: "DB Fail Client",
}),
/DATABASE_WRITE_FAILED/,
);
assert.equal(SDKManager.getClient("db_fail_client"), undefined);
} finally {
DatabaseEngine.getPool = origGetPool;
DatabaseEngine.ensureSchema = origEnsureSchema;
settingsGlobals.__dataStackAdminSettings = undefined;
settingsGlobals.__dataStackSettingsHydrated = false;
}
});
it("R2 PHASE-01 BUG-01: enforces same-origin CSRF check on POST /api/auth/login and POST /api/auth/setup while preserving same-origin and Bearer /api/events", async () => {
process.env.DATASTACK_ADMIN_SESSION_SECRET =
"test-secret-at-least-32-chars-long-for-session";
// Cross-origin POST /api/auth/login -> 403
const crossLoginRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "https://evil.example.com",
},
}),
);
assert.equal(crossLoginRes.status, 403);
assert.deepEqual(await crossLoginRes.json(), {
error: "Cross-origin request rejected.",
});
// Missing origin/referer/sec-fetch-site POST /api/auth/login -> 403
const bareLoginRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: { host: "localhost:3000" },
}),
);
assert.equal(bareLoginRes.status, 403);
// Cross-origin POST /api/auth/setup -> 403
const crossSetupRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "https://evil.example.com",
},
}),
);
assert.equal(crossSetupRes.status, 403);
// Missing origin POST /api/auth/setup -> 403
const bareSetupRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: { host: "localhost:3000" },
}),
);
assert.equal(bareSetupRes.status, 403);
// Same-origin POST /api/auth/login passes middleware (200 next)
const sameOriginLoginRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "http://localhost:3000",
},
}),
);
assert.equal(sameOriginLoginRes.status, 200);
// Privacy-stripped same-origin POST /api/auth/login via Sec-Fetch-Site passes middleware
const privacyLoginRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
host: "localhost:3000",
"sec-fetch-site": "same-origin",
},
}),
);
assert.equal(privacyLoginRes.status, 200);
// Same-origin POST /api/auth/setup passes middleware (200 next)
const sameOriginSetupRes = await middleware(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "http://localhost:3000",
},
}),
);
assert.equal(sameOriginSetupRes.status, 200);
// Bearer-authenticated POST /api/events without Origin stays allowed (machine-to-machine SDK)
const bearerEventsRes = await middleware(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
host: "localhost:3000",
authorization: "Bearer sdk.jwt.token",
},
}),
);
assert.equal(bearerEventsRes.status, 200);
// Non-Bearer cross-origin POST /api/events is rejected 403
const crossEventsRes = await middleware(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
host: "localhost:3000",
origin: "https://evil.example.com",
},
}),
);
assert.equal(crossEventsRes.status, 403);
// Safe GET public API endpoints remain accessible without Origin
const healthRes = await middleware(
new NextRequest("http://localhost:3000/api/health", {
method: "GET",
headers: { host: "localhost:3000" },
}),
);
assert.equal(healthRes.status, 200);
});
it("R2 PHASE-01 BUG-02: extractClientIp trusts X-Real-IP and rightmost X-Forwarded-For hop, blocking spoofed-XFF lockouts on login & setup", async () => {
// Direct unit assertions on extractClientIp
assert.equal(
extractClientIp(
new NextRequest("http://localhost:3000/api/auth/login", {
headers: {
"x-forwarded-for": "1.2.3.4, 203.0.113.9",
"x-real-ip": "203.0.113.9",
},
}),
),
"203.0.113.9",
);
assert.equal(
extractClientIp(
new NextRequest("http://localhost:3000/api/auth/login", {
headers: {
"x-forwarded-for": "1.2.3.4, 198.51.100.50, ",
"x-real-ip": " ",
},
}),
),
"198.51.100.50",
);
assert.equal(
extractClientIp(new NextRequest("http://localhost:3000/api/auth/login")),
"unknown",
);
// End-to-end brute-force lockout on POST /api/auth/login despite rotating spoofed leftmost XFF
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
process.env.DATASTACK_ADMIN_SETUP_KEY =
"phase01-setup-key-at-least-32-characters-long";
// First verify setup route blocks rotating spoofed leftmost XFF
for (let i = 0; i < 5; i++) {
const badSetup = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": `10.1.0.${i + 1}, 198.51.100.42`,
},
body: JSON.stringify({
email: "admin@datastack.dev",
password: "StrongPass123!",
setupKey: "wrong-key",
}),
}),
);
assert.equal(badSetup.status, 403);
}
const blockedSetup = await setupPost(
new NextRequest("http://localhost:3000/api/auth/setup", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "10.1.0.99, 198.51.100.42",
},
body: JSON.stringify({
email: "admin@datastack.dev",
password: "StrongPass123!",
setupKey: process.env.DATASTACK_ADMIN_SETUP_KEY,
}),
}),
);
assert.equal(blockedSetup.status, 429);
// Create admin and verify login route blocks rotating spoofed leftmost XFF with X-Real-IP
await createInitialAdmin("lockout@datastack.dev", "RealPassword123!");
for (let i = 0; i < 5; i++) {
const badLogin = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": `172.16.0.${i + 1}, 203.0.113.9`,
"x-real-ip": "203.0.113.9",
},
body: JSON.stringify({
email: "lockout@datastack.dev",
password: "WrongPassword!",
}),
}),
);
assert.equal(badLogin.status, 401);
}
const blockedLogin = await loginPost(
new NextRequest("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
"content-type": "application/json",
"x-forwarded-for": "172.16.0.99, 203.0.113.9",
"x-real-ip": "203.0.113.9",
},
body: JSON.stringify({
email: "lockout@datastack.dev",
password: "RealPassword123!",
}),
}),
);
assert.equal(blockedLogin.status, 429);
assert.ok(Number(blockedLogin.headers.get("retry-after")) >= 1);
});
});
+847
View File
@@ -1,20 +1,32 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import type { AddressInfo } from "node:net";
import { resolve } from "node:path";
import test, { after, before } from "node:test";
import { NextRequest } from "next/server";
import { io as ioClient, type Socket } from "socket.io-client";
import {
DELETE as deleteClientRoute,
GET as getClientRoute,
PATCH as patchClientRoute,
} from "../app/api/clients/[id]/route";
import { POST as postEventsRoute } from "../app/api/events/route";
import { GET as getSettingsRoute, POST as postSettingsRoute } from "../app/api/settings/route";
import { SettingsManager } from "../core/adminSettings/settingsManager";
import { AuthService } from "../core/auth/jwt";
import {
createAdminSessionToken,
getAdminSessionSecret,
} from "../core/auth/adminSession";
import {
createInitialAdmin,
isAdminDatabaseConfigured,
resetAdminStateForTests,
} from "../core/auth/adminUsers";
import { ApiEngine } from "../core/apiEngine/apiService";
import { DataBufferEngine, type RedisBufferClient } from "../core/data/buffer";
import { DatabaseEngine } from "../core/Database/database";
import { SDKManager } from "../core/sdk/sdkManager";
import {
SecurityEngine,
@@ -37,6 +49,15 @@ interface IngestionServerModule {
enableIdempotency: boolean;
autoPruneDays: number;
};
verifyClientInDb: (
clientId: string,
token: string,
bypass?: boolean,
) => Promise<{ allowed: boolean; reason?: string; rateLimit?: number }>;
recordClientActivity: (clientId: string, count?: number) => void;
flushPendingClientActivity: () => Promise<number>;
setRedisClientForTests: (client: unknown) => void;
setDbPoolForTests: (pool: unknown) => void;
stopTimers: () => void;
}
@@ -48,6 +69,9 @@ const g = globalThis as typeof globalThis & {
__dataStackClientsMap?: Map<string, unknown>;
__dataStackDeletedClients?: Set<string>;
__dataStackSyncedSocketEvents?: Map<string, number>;
__dataStackPendingClientActivity?: Map<string, unknown>;
__dataStackCorePool?: unknown;
__dataStackCoreSchema?: Promise<void>;
};
let serverMod: IngestionServerModule;
@@ -151,6 +175,7 @@ after(async () => {
clearInterval(g.__dataStackBufferState.flushTimer);
g.__dataStackBufferState.flushTimer = null;
}
SDKManager.stopActivityFlushTimerForTests();
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
@@ -175,6 +200,7 @@ test("BUG-13: Port 4000 /internal/* rejects unauthenticated callers and accepts
["GET", "/internal/recent", undefined],
["POST", "/internal/client-sync", { clientId: "x", action: "registered" }],
["POST", "/internal/settings-sync", { maxEventsPerSec: 120 }],
["POST", "/internal/security-check", { clientId: "x", eventId: "auth_chk_1", payloadBytes: 12 }],
] as const) {
const noAuth = await fetch(`${ingestionUrl}${path}`, {
method,
@@ -373,3 +399,824 @@ test("BUG-08: Rate-limit error reports effectiveRateLimit and ApiEngine aggregat
`Expected aggregated rateLimitedClientsCount >= 2, got ${metrics.securityStats.rateLimitedClientsCount}`,
);
});
function createSharedMockRedis(): RedisBufferClient {
const kv = new Map<string, string>();
const lists = new Map<string, string[]>();
const hashes = new Map<string, Map<string, string>>();
const sets = new Map<string, Set<string>>();
return {
status: "ready",
async get(key: string) {
return kv.has(key) ? kv.get(key)! : null;
},
async incr(key: string) {
const next = (Number(kv.get(key)) || 0) + 1;
kv.set(key, String(next));
return next;
},
async pexpire() {
return 1;
},
async set(key: string, value: string, _px?: "PX", _ttlMs?: number, nxMode?: "NX") {
if (nxMode === "NX" && kv.has(key)) return null;
kv.set(key, value);
return "OK";
},
async hget(key: string, field: string) {
return hashes.get(key)?.get(field) ?? null;
},
async hset(key: string, field: string, value: string) {
if (!hashes.has(key)) hashes.set(key, new Map());
hashes.get(key)!.set(field, value);
return 1;
},
async hdel(key: string, ...fields: string[]) {
let deleted = 0;
const h = hashes.get(key);
if (!h) return 0;
for (const f of fields) {
if (h.delete(f)) deleted += 1;
}
return deleted;
},
async sismember(key: string, member: string) {
return sets.get(key)?.has(member) ? 1 : 0;
},
async sadd(key: string, ...members: string[]) {
if (!sets.has(key)) sets.set(key, new Set());
for (const m of members) sets.get(key)!.add(m);
return members.length;
},
async srem(key: string, ...members: string[]) {
const s = sets.get(key);
if (!s) return 0;
let removed = 0;
for (const m of members) {
if (s.delete(m)) removed += 1;
}
return removed;
},
async rpush(key: string, ...values: string[]) {
const list = lists.get(key) ?? [];
list.push(...values);
lists.set(key, list);
return list.length;
},
async lpush(key: string, ...values: string[]) {
const list = lists.get(key) ?? [];
list.unshift(...values);
lists.set(key, list);
return list.length;
},
async ltrim(key: string, start: number, stop: number) {
const list = lists.get(key) ?? [];
const len = list.length;
const end = stop < 0 ? len + stop : stop;
lists.set(key, list.slice(start, end + 1));
return "OK";
},
async lrange(key: string, start: number, stop: number) {
const list = lists.get(key) ?? [];
const len = list.length;
const end = stop < 0 ? len + stop : stop;
return list.slice(start, end + 1);
},
async llen(key: string) {
return (lists.get(key) ?? []).length;
},
async eval(script: string, _numKeys: number, ...args: string[]) {
if (script.includes("DATASTACK_RATE_LIMIT_INCR")) {
const key = args[0];
const next = (Number(kv.get(key)) || 0) + 1;
kv.set(key, String(next));
return next;
}
if (script.includes("DATASTACK_PURGE_CLIENT_LIST")) {
const [key, target] = args;
const list = lists.get(key) ?? [];
const kept: string[] = [];
let removed = 0;
for (const raw of list) {
try {
const parsed = JSON.parse(raw) as { clientId?: string };
const cid = String(parsed?.clientId || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]/g, "_");
if (cid === target) {
removed += 1;
continue;
}
} catch {
// preserve unparseable entries
}
kept.push(raw);
}
if (removed > 0) lists.set(key, kept);
return removed;
}
return 1;
},
};
}
test("BUG-B01: Port 4000 verifyClientInDb fails closed for unregistered/empty clients even without DB", async () => {
const emptyCheck = await serverMod.verifyClientInDb("", "any-token", true);
assert.equal(emptyCheck.allowed, false);
assert.match(emptyCheck.reason || "", /CLIENT_REVOKED/);
const ghostCheck = await serverMod.verifyClientInDb("Unregistered Ghost", "any-token", true);
assert.equal(ghostCheck.allowed, false);
assert.match(ghostCheck.reason || "", /CLIENT_REVOKED/);
const ghostToken = AuthService.generateClientToken({ clientId: "never_registered_ws" });
await assert.rejects(() => connectSocket(ghostToken), /CLIENT_REVOKED/);
});
test("BUG-B02 & BUG-B04: Shared Redis idempotency/rate-limit across Port 3000+4000 and flat sync_data payload parity", async () => {
const mockRedis = createSharedMockRedis();
DataBufferEngine.setRedisClientForTests(mockRedis);
serverMod.setRedisClientForTests(mockRedis);
try {
await SettingsManager.updateSettingsAsync({
maxEventsPerSec: 3,
enableIdempotency: true,
});
const client = await SDKManager.registerClientAsync({
clientId: "phase02_redis_parity",
name: "Phase02 Redis Parity",
rateLimit: 3,
});
// 1. Flat sync_data payload (no .data wrapper) preserves fields cleanly (BUG-B04)
serverMod.bufferQueue.length = 0;
const socket = await connectSocket(client.token);
const flatAck = await emitWithAck<{ success: boolean; eventId: string }>(
socket,
"sync_data",
{ eventId: "evt_shared_flat_1", metric: "voltage", value: 3.3 },
);
assert.equal(flatAck.success, true);
const bufferedItems = await mockRedis.lrange("datastack:buffer:events", 0, -1);
const storedFlat = bufferedItems
.map((raw) => JSON.parse(raw) as { eventId: string; payload: string })
.find((e) => e.eventId === "evt_shared_flat_1");
assert.ok(storedFlat, "Flat sync_data event must be buffered in Redis");
const parsedFlat = JSON.parse(storedFlat.payload) as Record<string, unknown>;
assert.equal(parsedFlat.metric, "voltage");
assert.equal(parsedFlat.value, 3.3);
// 2. Replaying same eventId over HTTP POST /api/events triggers shared DUPLICATE_EVENT (BUG-B02)
const replayHttpRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
eventId: "evt_shared_flat_1",
data: { metric: "voltage", value: 3.3 },
}),
}),
);
assert.equal(replayHttpRes.status, 200);
const replayHttpJson = await replayHttpRes.json();
assert.equal(replayHttpJson.note, "DUPLICATE_DROPPED");
// 3. Cross-runtime shared per-second rate-limit: 1 WS already consumed quota; 2 HTTP pass, 3rd hits 429 (BUG-B02)
const ok2 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_rl_shared_2", data: { n: 2 } }),
}),
);
const ok3 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_rl_shared_3", data: { n: 3 } }),
}),
);
assert.equal(ok2.status, 200);
assert.equal(ok3.status, 200);
const limited4 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_rl_shared_4", data: { n: 4 } }),
}),
);
assert.equal(limited4.status, 429);
socket.disconnect();
} finally {
DataBufferEngine.setRedisClientForTests(undefined);
serverMod.setRedisClientForTests(undefined);
}
});
test("BUG-B03 & BUG-B05: Surfacing bridge sync failures, live runtime ports, normalized counter & production start script", async () => {
const cookieHeader = await createAdminCookieHeader();
const res = await postSettingsRoute(
new NextRequest("http://localhost:3000/api/settings", {
method: "POST",
headers: { "content-type": "application/json", cookie: cookieHeader },
body: JSON.stringify({ maxEventsPerSec: 120 }),
}),
);
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.bridgeSync?.synced, true);
// Mismatched secret on remote Port 4000 must surface auth_error on dashboard metrics
const http = await import("node:http");
const rejectServer = http.createServer((_req, res) => {
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ success: false, code: "UNAUTHORIZED" }));
});
await new Promise<void>((resolveListen) => rejectServer.listen(0, "127.0.0.1", resolveListen));
const rejectAddr = rejectServer.address() as AddressInfo;
const savedIngestionUrl = process.env.INGESTION_INTERNAL_URL;
process.env.INGESTION_INTERNAL_URL = `http://127.0.0.1:${rejectAddr.port}`;
try {
const metrics = await ApiEngine.getDashboardMetrics();
assert.equal(metrics.runtime.ingestionBridgeStatus, "auth_error");
assert.match(metrics.runtime.ingestionBridgeError || "", /HTTP 401/);
} finally {
process.env.INGESTION_INTERNAL_URL = savedIngestionUrl;
await new Promise<void>((resolveClose) => rejectServer.close(() => resolveClose()));
}
// Normalized totalEvents counter updates cleanly
const normClient = await SDKManager.registerClientAsync({
clientId: "Mixed.Case.Counter",
name: "Counter Check",
rateLimit: 100,
});
await SDKManager.incrementEventCountAsync("MIXED.CASE.COUNTER", 3);
const fetched = await SDKManager.getClientAsync(normClient.clientId);
assert.equal(fetched?.totalEvents, 3);
// Production start script runs dual-runtime launcher
const pkg = JSON.parse(readFileSync(resolve(process.cwd(), "package.json"), "utf8")) as {
scripts?: Record<string, string>;
};
assert.equal(pkg.scripts?.start, "node scripts/start-all.js");
assert.equal(pkg.scripts?.["start:server"], "node server/index.js");
});
test("FORENSIC BUG-02: Empty Port 3000 bulk_sync preserves Port 4000 registry and hydrates Port 3000 non-destructively", async () => {
const survivor = await SDKManager.registerClientAsync({
clientId: "phase02_survivor_client",
name: "Survivor Client",
rateLimit: 40,
});
// Verify Port 4000 knows survivor before simulated Port 3000 restart
const preCheck = await serverMod.verifyClientInDb(survivor.clientId, survivor.token, true);
assert.equal(preCheck.allowed, true);
// Simulate Port 3000 cold restart without PostgreSQL (local map cleared)
g.__dataStackClientsMap?.clear();
assert.equal(SDKManager.getClient(survivor.clientId), undefined);
// Calling syncAllClientsToIngestionAsync() with an empty Port 3000 map must NOT wipe Port 4000
const syncRes = await SDKManager.syncAllClientsToIngestionAsync();
assert.equal(syncRes.synced, true);
const postCheck = await serverMod.verifyClientInDb(survivor.clientId, survivor.token, true);
assert.equal(postCheck.allowed, true, "Port 4000 registry must NOT be wiped by empty Port 3000 bulk_sync");
// Port 3000 should also have hydrated non-destructively from Port 4000
const hydratedAfterBulk = SDKManager.getClient(survivor.clientId);
assert.ok(hydratedAfterBulk, "Port 3000 should hydrate preserved client after empty bulk_sync");
assert.equal(hydratedAfterBulk.token, survivor.token);
// Also test cold hydration via getClientAsync & listClientsAsync after clearing map again
g.__dataStackClientsMap?.clear();
const hydratedViaGet = await SDKManager.getClientAsync(survivor.clientId);
assert.ok(hydratedViaGet, "getClientAsync should non-destructively hydrate from Port 4000");
assert.equal(hydratedViaGet.clientId, survivor.clientId);
});
test("FORENSIC BUG-17: JWT rotation coordinates Port 4000 first, aborts on bridge rejection without split-brain, and supports rotation grace", async () => {
const originalSecret = AuthService.getSecret();
const http = await import("node:http");
// 1. If Port 4000 rejects /internal/settings-sync with 503, Port 3000 MUST abort rotation cleanly
const rejectServer = http.createServer((_req, res) => {
res.writeHead(503, { "Content-Type": "application/json" });
res.end(JSON.stringify({ success: false, code: "BRIDGE_REJECTED" }));
});
await new Promise<void>((r) => rejectServer.listen(0, "127.0.0.1", r));
const rejectPort = (rejectServer.address() as AddressInfo).port;
const savedUrl = process.env.INGESTION_INTERNAL_URL;
process.env.INGESTION_INTERNAL_URL = `http://127.0.0.1:${rejectPort}`;
try {
await assert.rejects(
() =>
SettingsManager.updateSettingsAsync({
jwtSecret: "aborted-rotation-secret-must-never-commit-32-chars",
}),
/JWT_ROTATION_BRIDGE_FAILED/,
);
assert.equal(
AuthService.getSecret(),
originalSecret,
"Port 3000 AuthService secret must remain unchanged when Port 4000 rejects rotation",
);
assert.equal(
SettingsManager.getSettings().jwtSecret,
originalSecret,
"SettingsManager secret must remain unchanged after aborted rotation",
);
} finally {
process.env.INGESTION_INTERNAL_URL = savedUrl;
await new Promise<void>((r) => rejectServer.close(() => r()));
}
// 2. Successful rotation updates both runtimes AND keeps previous secret valid on Port 4000 bridge grace window
const rotatedSecret = "phase02-bug17-coordinated-rotation-secret-32-chars";
await SettingsManager.updateSettingsAsync({ jwtSecret: rotatedSecret });
assert.equal(AuthService.getSecret(), rotatedSecret);
assert.equal(serverMod.runtimeConfig.jwtSecret, rotatedSecret);
const graceHeaders = getInternalIngestionHeaders(originalSecret);
const graceMetricsRes = await fetch(`${ingestionUrl}/internal/metrics`, {
headers: graceHeaders,
});
assert.equal(
graceMetricsRes.status,
200,
"Port 4000 internal auth must accept previous secret during rotation grace window",
);
// 3. New secret also works on internal bridge
const currentMetricsRes = await fetch(`${ingestionUrl}/internal/metrics`, {
headers: getInternalIngestionHeaders(rotatedSecret),
});
assert.equal(currentMetricsRes.status, 200);
// 4. If PG write fails AFTER Port 4000 accepts the new secret, BOTH Port 3000 & Port 4000 roll back cleanly
const prevDbUrlLocal = process.env.DATABASE_URL;
const prevPoolLocal = g.__dataStackCorePool;
const prevSchemaLocal = g.__dataStackCoreSchema;
process.env.DATABASE_URL = "postgres://mock:mock@127.0.0.1:5432/mock";
g.__dataStackCoreSchema = Promise.resolve();
g.__dataStackCorePool = {
async query(sql: string) {
if (sql.includes("INSERT INTO admin_settings")) {
throw new Error("Simulated PG disk full during rotation");
}
return { rowCount: 0, rows: [] };
},
};
try {
await assert.rejects(
() =>
SettingsManager.updateSettingsAsync({
jwtSecret: "rolled-back-secret-after-pg-failure-32-chars-min",
}),
/DATABASE_WRITE_FAILED/,
);
assert.equal(
AuthService.getSecret(),
rotatedSecret,
"Port 3000 must roll back to previous secret after DB write failure",
);
assert.equal(
serverMod.runtimeConfig.jwtSecret,
rotatedSecret,
"Port 4000 must roll back to previous secret after Port 3000 DB write failure",
);
} finally {
if (prevDbUrlLocal === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrlLocal;
g.__dataStackCorePool = prevPoolLocal;
g.__dataStackCoreSchema = prevSchemaLocal;
}
});
test("FORENSIC BUG-03: Without Redis, WS and HTTP share idempotency and rate limits via Port 4000 /internal/security-check", async () => {
// Explicitly ensure Redis is disabled
DataBufferEngine.setRedisClientForTests(null);
serverMod.setRedisClientForTests(null);
try {
await SettingsManager.updateSettingsAsync({
maxEventsPerSec: 3,
enableIdempotency: true,
});
const client = await SDKManager.registerClientAsync({
clientId: "phase02_no_redis_sec",
name: "No-Redis Shared Security",
rateLimit: 3,
});
const socket = await connectSocket(client.token);
const wsAck = await emitWithAck<{ success: boolean; eventId: string }>(
socket,
"sync_data",
{ eventId: "evt_noredis_shared_1", data: { step: 1 } },
);
assert.equal(wsAck.success, true);
// Same eventId over HTTP POST /api/events MUST be deduplicated via Port 4000 bridge check
const dupHttpRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
eventId: "evt_noredis_shared_1",
data: { step: 1 },
}),
}),
);
assert.equal(dupHttpRes.status, 200);
const dupJson = await dupHttpRes.json();
assert.equal(
dupJson.note,
"DUPLICATE_DROPPED",
"HTTP replay of WS eventId must be dropped even without Redis",
);
// Rate-limit parity without Redis: WS consumed 1 of 3; 2 HTTP pass, 3rd returns 429
const http2 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_noredis_rl_2", data: { step: 2 } }),
}),
);
const http3 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_noredis_rl_3", data: { step: 3 } }),
}),
);
assert.equal(http2.status, 200);
assert.equal(http3.status, 200);
const http4 = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ eventId: "evt_noredis_rl_4", data: { step: 4 } }),
}),
);
assert.equal(http4.status, 429, "Combined WS+HTTP quota must enforce 429 without Redis");
// Metrics explicitly surface bridge_coordinated securityMode
const metrics = await ApiEngine.getDashboardMetrics();
assert.equal(metrics.securityStats.mode, "bridge_coordinated");
assert.equal(metrics.runtime.securityMode, "bridge_coordinated");
socket.disconnect();
} finally {
DataBufferEngine.setRedisClientForTests(undefined);
serverMod.setRedisClientForTests(undefined);
}
});
test("FORENSIC BUG-22: High-frequency client event increments coalesce into batched UNNEST SQL updates on both Port 3000 and Port 4000", async () => {
const sdkQueries: Array<{ sql: string; params: unknown[] }> = [];
const serverQueries: Array<{ sql: string; params: unknown[] }> = [];
const mockSdkPool = {
async query(sql: string, params: unknown[] = []) {
sdkQueries.push({ sql, params });
return { rowCount: 1, rows: [] };
},
};
const mockServerPool = {
async query(sql: string, params: unknown[] = []) {
serverQueries.push({ sql, params });
return { rowCount: 1, rows: [] };
},
};
const prevDatabaseUrl = process.env.DATABASE_URL;
const prevCorePool = g.__dataStackCorePool;
const prevCoreSchema = g.__dataStackCoreSchema;
process.env.DATABASE_URL = "postgres://mock:mock@127.0.0.1:5432/mock";
g.__dataStackCorePool = mockSdkPool;
g.__dataStackCoreSchema = Promise.resolve();
g.__dataStackPendingClientActivity?.clear();
serverMod.setDbPoolForTests(mockServerPool);
try {
// 1. Port 3000 SDKManager: 25 rapid increments across 2 clients -> 0 immediate per-event queries, 1 batched UNNEST query on flush
for (let i = 0; i < 15; i += 1) {
await SDKManager.incrementEventCountAsync("batch_client_a", 1);
}
for (let i = 0; i < 10; i += 1) {
await SDKManager.incrementEventCountAsync("batch_client_b", 2);
}
const preFlushUpdates = sdkQueries.filter((q) =>
q.sql.includes("UPDATE client_sdks"),
);
assert.equal(
preFlushUpdates.length,
0,
"Per-event increments must coalesce in memory instead of firing immediate per-row SQL",
);
const flushedSdkClients = await SDKManager.flushPendingClientActivityAsync();
assert.equal(flushedSdkClients, 2);
const postFlushSdkUpdates = sdkQueries.filter((q) =>
q.sql.includes("UPDATE client_sdks"),
);
assert.equal(
postFlushSdkUpdates.length,
1,
"25 increments across 2 clients must execute exactly ONE batched UPDATE query",
);
assert.match(postFlushSdkUpdates[0].sql, /UNNEST\(\$1::text\[\]\)/);
assert.deepEqual(postFlushSdkUpdates[0].params[0], [
"batch_client_a",
"batch_client_b",
]);
assert.deepEqual(postFlushSdkUpdates[0].params[1], [15, 20]);
// 2. Port 4000 server/index.js: 30 rapid recordClientActivity calls -> 1 batched UNNEST query on flush
for (let i = 0; i < 20; i += 1) {
serverMod.recordClientActivity("ws_batch_a", 1);
}
for (let i = 0; i < 10; i += 1) {
serverMod.recordClientActivity("ws_batch_b", 3);
}
assert.equal(
serverQueries.length,
0,
"Port 4000 recordClientActivity must coalesce instead of running per-event SQL",
);
const flushedServerClients = await serverMod.flushPendingClientActivity();
assert.equal(flushedServerClients, 2);
assert.equal(
serverQueries.length,
1,
"30 Port 4000 activity events must coalesce into ONE batched UPDATE query",
);
assert.match(serverQueries[0].sql, /UNNEST\(\$1::text\[\]\)/);
assert.deepEqual(serverQueries[0].params[0], ["ws_batch_a", "ws_batch_b"]);
assert.deepEqual(serverQueries[0].params[1], [20, 30]);
} finally {
SDKManager.stopActivityFlushTimerForTests();
g.__dataStackPendingClientActivity?.clear();
serverMod.setDbPoolForTests(null);
if (prevDatabaseUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDatabaseUrl;
g.__dataStackCorePool = prevCorePool;
g.__dataStackCoreSchema = prevCoreSchema;
}
});
test("R2 PHASE-02 BUG-05: PG pools attach safe error listeners and Port 3000 shares a single unified DatabaseEngine pool", async () => {
const prevDbUrl = process.env.DATABASE_URL;
const prevCorePool = g.__dataStackCorePool;
const origConsoleError = console.error;
const loggedErrors: string[] = [];
console.error = (...args: unknown[]) => {
loggedErrors.push(args.map((a) => String(a)).join(" "));
};
try {
process.env.DATABASE_URL = "postgres://unit:test@127.0.0.1:5432/datastack_unit";
g.__dataStackCorePool = undefined;
const pool = DatabaseEngine.getPool();
assert.ok(pool, "DatabaseEngine.getPool() must instantiate a pool when DATABASE_URL is set");
assert.ok(
pool.listenerCount("error") >= 1,
"DatabaseEngine PG pool must attach at least one 'error' listener",
);
assert.equal(
isAdminDatabaseConfigured(),
true,
"adminUsers must delegate to the shared DatabaseEngine.getPool()",
);
// Emitting an idle client error must log safely instead of throwing an uncaught EventEmitter crash
assert.doesNotThrow(() => {
pool.emit("error", new Error("simulated idle PG restart"));
});
assert.ok(
loggedErrors.some((msg) => msg.includes("simulated idle PG restart")),
"Pool error listener must log unexpected idle pool errors",
);
// Static verification that adminUsers.ts has zero independent `new Pool` and server/index.js attaches dbPool.on('error')
const adminUsersSrc = readFileSync(
resolve(process.cwd(), "core", "auth", "adminUsers.ts"),
"utf8",
);
assert.equal(/\bnew Pool\s*\(/.test(adminUsersSrc), false);
const serverSrc = readFileSync(
resolve(process.cwd(), "server", "index.js"),
"utf8",
);
assert.match(serverSrc, /dbPool\.on\(\s*['"]error['"]/);
await pool.end().catch(() => undefined);
} finally {
console.error = origConsoleError;
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
g.__dataStackCorePool = prevCorePool;
}
});
test("R2 PHASE-02 BUG-09: Redis rate-limiting uses atomic single-roundtrip Lua EVAL on both Port 3000 and Port 4000", async () => {
const baseRedis = createSharedMockRedis();
let evalRateLimitCalls = 0;
let separateIncrCalls = 0;
let separatePexpireCalls = 0;
const instrumentedRedis: RedisBufferClient = {
...baseRedis,
async incr(key: string) {
separateIncrCalls += 1;
return baseRedis.incr!(key);
},
async pexpire(key: string, ttlMs: number) {
separatePexpireCalls += 1;
return baseRedis.pexpire!(key, ttlMs);
},
async eval(script: string, numKeys: number, ...args: string[]) {
if (script.includes("DATASTACK_RATE_LIMIT_INCR")) {
evalRateLimitCalls += 1;
assert.equal(args[1], "2000", "Atomic Lua rate-limit script must pass 2000ms TTL");
}
return baseRedis.eval(script, numKeys, ...args);
},
};
DataBufferEngine.setRedisClientForTests(instrumentedRedis);
serverMod.setRedisClientForTests(instrumentedRedis);
try {
await SettingsManager.updateSettingsAsync({
maxEventsPerSec: 2,
enableIdempotency: true,
});
const client = await SDKManager.registerClientAsync({
clientId: "phase02_lua_rl_client",
name: "Lua RL Client",
rateLimit: 2,
});
// 1 event via Port 3000 HTTP SecurityEngine
const http1 = await SecurityEngine.validateEventAsync(
client.clientId,
"lua_rl_1",
{ v: 1 },
client.rateLimit,
);
assert.equal(http1.allowed, true);
// 2nd event via Port 4000 Socket.IO handler (allowed) + 3rd event (rate limited)
const socket = await connectSocket(client.token);
const ws2 = await emitWithAck<{ success: boolean; code?: string }>(
socket,
"sync_data",
{ eventId: "lua_rl_2", data: { v: 2 } },
);
assert.equal(ws2.success, true);
const ws3 = await emitWithAck<{ success: boolean; code?: string }>(
socket,
"sync_data",
{ eventId: "lua_rl_3", data: { v: 3 } },
);
assert.equal(ws3.success, false);
assert.equal(ws3.code, "RATE_LIMIT_EXCEEDED");
socket.disconnect();
assert.equal(
evalRateLimitCalls,
3,
"All 3 rate-limit checks across Port 3000 & Port 4000 must execute atomic Lua EVAL",
);
assert.equal(
separateIncrCalls,
0,
"Separate non-atomic redis.incr must NOT be called when redis.eval is available",
);
assert.equal(
separatePexpireCalls,
0,
"Separate non-atomic redis.pexpire must NOT be called when redis.eval is available",
);
} finally {
DataBufferEngine.setRedisClientForTests(undefined);
serverMod.setRedisClientForTests(undefined);
await SettingsManager.updateSettingsAsync({ maxEventsPerSec: 100 });
}
});
test("R2 PHASE-02 BUG-10: GET, PATCH (regenerate_token), and DELETE /api/clients/[id] catch DB errors and return structured JSON", async () => {
const secret = getAdminSessionSecret()!;
const adminToken = await createAdminSessionToken(
"admin@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
const cookieHeader = `datastack_admin_session=${adminToken}`;
const origGet = SDKManager.getClientAsync;
const origRegen = SDKManager.regenerateTokenAsync;
const origDeleteWithStats = SDKManager.deleteClientWithStatsAsync;
try {
SDKManager.getClientAsync = async () => {
throw new Error("DATABASE_READ_FAILED: simulated pg read outage");
};
const getRes = await getClientRoute(
new NextRequest("http://localhost:3000/api/clients/broken_client", {
method: "GET",
headers: { cookie: cookieHeader },
}),
{ params: Promise.resolve({ id: "broken_client" }) },
);
assert.equal(getRes.status, 503);
const getJson = await getRes.json();
assert.equal(getJson.code, "DATABASE_READ_FAILED");
assert.match(getJson.error, /simulated pg read outage/);
SDKManager.regenerateTokenAsync = async () => {
throw new Error("DATABASE_WRITE_FAILED: simulated token rotation DB failure");
};
const regenRes = await patchClientRoute(
new NextRequest("http://localhost:3000/api/clients/broken_client", {
method: "PATCH",
headers: {
"content-type": "application/json",
cookie: cookieHeader,
},
body: JSON.stringify({ action: "regenerate_token" }),
}),
{ params: Promise.resolve({ id: "broken_client" }) },
);
assert.equal(regenRes.status, 503);
const regenJson = await regenRes.json();
assert.equal(regenJson.code, "DATABASE_WRITE_FAILED");
assert.match(regenJson.error, /simulated token rotation DB failure/);
SDKManager.deleteClientWithStatsAsync = async () => {
throw new Error("DATABASE_WRITE_FAILED: simulated delete DB failure");
};
const delRes = await deleteClientRoute(
new NextRequest("http://localhost:3000/api/clients/broken_client", {
method: "DELETE",
headers: { cookie: cookieHeader },
}),
{ params: Promise.resolve({ id: "broken_client" }) },
);
assert.equal(delRes.status, 503);
const delJson = await delRes.json();
assert.equal(delJson.code, "DATABASE_WRITE_FAILED");
assert.match(delJson.error, /simulated delete DB failure/);
} finally {
SDKManager.getClientAsync = origGet;
SDKManager.regenerateTokenAsync = origRegen;
SDKManager.deleteClientWithStatsAsync = origDeleteWithStats;
}
});
File diff suppressed because it is too large. Load diff
+495
View File
@@ -0,0 +1,495 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import { createRequire } from "node:module";
import os from "node:os";
import path from "node:path";
import test, { after, before, beforeEach } from "node:test";
import { NextRequest } from "next/server";
import { GET as getDataRoute } from "../app/api/data/route";
import {
GET as getEventsRoute,
POST as postEventsRoute,
} from "../app/api/events/route";
import {
createAdminSessionToken,
getAdminSessionSecret,
} from "../core/auth/adminSession";
import {
createInitialAdmin,
resetAdminStateForTests,
} from "../core/auth/adminUsers";
import {
DataBufferEngine,
MAX_REDIS_RECENT_LIMIT,
resolveRedisRecentLimit,
} from "../core/data/buffer";
import {
CORE_SCHEMA_ALTER_DDLS,
CORE_SCHEMA_TABLE_DDLS,
} from "../core/Database/database";
import { SDKManager } from "../core/sdk/sdkManager";
import { CSVStorageManager, type DataEvent } from "../core/Storage/csvStorage";
const g = globalThis as typeof globalThis & {
__dataStackBufferState?: {
buffer: DataEvent[];
recentEvents: DataEvent[];
deletedClientIds?: Set<string>;
batchFlushThreshold: number;
};
__dataStackAdminSettings?: unknown;
__dataStackClientsMap?: Map<string, unknown>;
__dataStackDeletedClients?: Set<string>;
};
let tmpStorageDir = "";
let adminCookieHeader = "";
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
const prevDbUrl = process.env.DATABASE_URL;
const prevRedisUrl = process.env.REDIS_URL;
const prevRecentEnv = process.env.DATASTACK_RECENT_LIMIT;
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
before(async () => {
delete process.env.DATABASE_URL;
delete process.env.REDIS_URL;
delete process.env.DATASTACK_RECENT_LIMIT;
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
resetAdminStateForTests();
await createInitialAdmin("admin@datastack.dev", "Phase04Pass123!");
const secret = getAdminSessionSecret()!;
const token = await createAdminSessionToken(
"admin@datastack.dev",
secret,
Date.now() + 3600_000,
1,
);
adminCookieHeader = `datastack_admin_session=${token}`;
tmpStorageDir = fs.mkdtempSync(path.join(os.tmpdir(), "datastack-phase04-"));
CSVStorageManager.baseStorageDir = tmpStorageDir;
CSVStorageManager.clearRowCountCache();
});
beforeEach(() => {
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
DataBufferEngine.setRedisClientForTests(null);
DataBufferEngine.batchFlushThreshold = 500;
delete process.env.DATASTACK_RECENT_LIMIT;
if (g.__dataStackBufferState) {
g.__dataStackBufferState.buffer = [];
g.__dataStackBufferState.recentEvents = [];
g.__dataStackBufferState.deletedClientIds?.clear();
}
CSVStorageManager.clearRowCountCache();
fs.rmSync(tmpStorageDir, { recursive: true, force: true });
fs.mkdirSync(tmpStorageDir, { recursive: true });
delete g.__dataStackAdminSettings;
g.__dataStackClientsMap?.clear();
g.__dataStackDeletedClients?.clear();
});
after(async () => {
await DataBufferEngine.shutdown();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
CSVStorageManager.clearRowCountCache();
if (tmpStorageDir) {
fs.rmSync(tmpStorageDir, { recursive: true, force: true });
}
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
if (prevRedisUrl === undefined) delete process.env.REDIS_URL;
else process.env.REDIS_URL = prevRedisUrl;
if (prevRecentEnv === undefined) delete process.env.DATASTACK_RECENT_LIMIT;
else process.env.DATASTACK_RECENT_LIMIT = prevRecentEnv;
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
});
test("FORENSIC BUG-21 (Part A): HTTP POST /api/events matches WS envelope stripping parity and never fabricates sample telemetry for {}", async () => {
const client = await SDKManager.registerClientAsync({
clientId: "phase04_parity",
name: "Parity Client",
rateLimit: 100,
});
// 1. Empty {} body must store "{}" (NO fake { sample, rand })
const emptyRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({}),
}),
);
assert.equal(emptyRes.status, 200);
const emptyJson = await emptyRes.json();
assert.equal(emptyJson.success, true);
assert.equal(emptyJson.event.payload, "{}");
assert.equal(emptyJson.event.payload.includes("sample"), false);
assert.equal(emptyJson.event.payload.includes("rand"), false);
// 2. Flat body with envelope keys must strip eventId/timestamp/clientId cleanly
const flatRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
eventId: "flat-parity-1",
timestamp: "2026-11-19T12:00:00.000Z",
clientId: client.clientId,
temp: 24.5,
unit: "C",
}),
}),
);
assert.equal(flatRes.status, 200);
const flatJson = await flatRes.json();
assert.deepEqual(JSON.parse(flatJson.event.payload), { temp: 24.5, unit: "C" });
// 3. Pure envelope { eventId, timestamp, data } extracts data identically to WS
const wrappedRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
eventId: "wrapped-parity-1",
timestamp: "2026-11-19T12:00:01.000Z",
data: { pressure: 101.3 },
}),
}),
);
assert.equal(wrappedRes.status, 200);
const wrappedJson = await wrappedRes.json();
assert.deepEqual(JSON.parse(wrappedJson.event.payload), { pressure: 101.3 });
// 4. Flat payload that contains both `data` and extra domain fields preserves the object identically to WS
const mixedRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
eventId: "mixed-parity-1",
data: "sensor-raw",
extraMetric: 42,
}),
}),
);
assert.equal(mixedRes.status, 200);
const mixedJson = await mixedRes.json();
assert.deepEqual(JSON.parse(mixedJson.event.payload), {
eventId: "mixed-parity-1",
data: "sensor-raw",
extraMetric: 42,
});
});
test("FORENSIC BUG-21 (Part B): GET /api/data uses non-blocking async fs.promises with partition pruning and REDIS_RECENT_LIMIT scales boundedly", async () => {
const dataRouteSrc = fs.readFileSync(
path.resolve(process.cwd(), "app/api/data/route.ts"),
"utf8",
);
assert.match(dataRouteSrc, /from "node:fs\/promises"/);
assert.equal(/\breaddirSync\b/.test(dataRouteSrc), false);
assert.equal(/\blstatSync\b/.test(dataRouteSrc), false);
// Create an out-of-range corrupt CSV partition and an in-range healthy CSV partition
const clientDir = CSVStorageManager.ensureClientDir("phase04_async");
fs.writeFileSync(
path.join(clientDir, "2026-11-10.csv"),
'"clientId","eventId","timestamp","payload"\n"phase04_async","bad","2026-11-10T00:00:00Z","unclosed',
"utf8",
);
await CSVStorageManager.appendBatch([
{
clientId: "phase04_async",
eventId: "in-range-1",
timestamp: "2026-11-19T14:00:00.000Z",
payload: JSON.stringify({ healthy: true }),
},
]);
const res = await getDataRoute(
new NextRequest(
"http://localhost:3000/api/data?clientId=phase04_async&from=2026-11-19&to=2026-11-19",
{ headers: { cookie: adminCookieHeader } },
),
);
assert.equal(res.status, 200);
const json = await res.json();
assert.equal(json.total, 1);
assert.equal(json.events[0].eventId, "in-range-1");
// Bounded configurable recent limit scaling
assert.equal(resolveRedisRecentLimit(500), 500);
assert.equal(resolveRedisRecentLimit(1200), 1200);
assert.equal(resolveRedisRecentLimit(50000), MAX_REDIS_RECENT_LIMIT);
process.env.DATASTACK_RECENT_LIMIT = "900";
assert.equal(DataBufferEngine.getRecentLimit(), 900);
delete process.env.DATASTACK_RECENT_LIMIT;
DataBufferEngine.batchFlushThreshold = 650;
assert.equal(DataBufferEngine.getRecentLimit(), 650);
const batch600: DataEvent[] = Array.from({ length: 600 }, (_, i) => ({
clientId: "phase04_recent_scale",
eventId: `recent-scale-${i}`,
timestamp: "2026-11-19T15:00:00.000Z",
payload: JSON.stringify({ i }),
}));
await DataBufferEngine.pushBatchAsync(batch600);
assert.equal(DataBufferEngine.getRecentEvents(650, "phase04_recent_scale").length, 600);
});
test("FORENSIC BUG-23: Client secrets and fallback eventIds use CSPRNG crypto.randomBytes with zero Math.random()", async () => {
const client = await SDKManager.registerClientAsync({
clientId: "phase04_csprng",
name: "CSPRNG Client",
rateLimit: 100,
});
assert.match(client.secret, /^sec_phase04_csprng_[0-9a-f]{32}$/);
const rotated = await SDKManager.regenerateTokenAsync(client.clientId);
assert.ok(rotated);
assert.match(rotated.secret, /^sec_phase04_csprng_[0-9a-f]{32}$/);
assert.notEqual(rotated.secret, client.secret);
const res = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${rotated.token}`,
},
body: JSON.stringify({ metric: "ok" }),
}),
);
assert.equal(res.status, 200);
const json = await res.json();
assert.match(json.eventId, /^evt_\d+_[0-9a-f]{12}$/);
for (const relPath of [
"core/sdk/sdkManager.ts",
"app/api/events/route.ts",
"server/socket/handlers.js",
]) {
const src = fs.readFileSync(path.resolve(process.cwd(), relPath), "utf8");
assert.equal(
/\bMath\.random\s*\(/.test(src),
false,
`Expected zero Math.random() calls in ${relPath}`,
);
}
});
test("FORENSIC BUG-24: Canonical PostgreSQL DDL is unified in database.ts, delegated by adminUsers.ts, and mirrored 1:1 in server/index.js & prisma/schema.prisma", () => {
const adminUsersSrc = fs.readFileSync(
path.resolve(process.cwd(), "core/auth/adminUsers.ts"),
"utf8",
);
const serverIndexSrc = fs.readFileSync(
path.resolve(process.cwd(), "server/index.js"),
"utf8",
);
const prismaSchemaSrc = fs.readFileSync(
path.resolve(process.cwd(), "prisma/schema.prisma"),
"utf8",
);
// adminUsers.ts must delegate to ensureCoreSchemaOnPool rather than duplicating CREATE TABLE
assert.match(adminUsersSrc, /ensureCoreSchemaOnPool/);
assert.equal(/CREATE TABLE IF NOT EXISTS/.test(adminUsersSrc), false);
// All 5 canonical tables must exist identically in database.ts, server/index.js, and prisma/schema.prisma
const expectedTables = [
"client_sdks",
"admin_settings",
"deleted_clients",
"admin_users",
"admin_login_attempts",
];
const dbTablesText = CORE_SCHEMA_TABLE_DDLS.join("\n");
for (const table of expectedTables) {
const createRe = new RegExp(`CREATE TABLE IF NOT EXISTS\\s+${table}\\b`);
assert.match(dbTablesText, createRe, `database.ts missing ${table}`);
assert.match(serverIndexSrc, createRe, `server/index.js missing ${table}`);
assert.match(
prismaSchemaSrc,
new RegExp(`@@map\\("${table}"\\)`),
`prisma/schema.prisma missing @@map("${table}")`,
);
}
// All 3 additive ALTER TABLE guards must exist in both database.ts and server/index.js
const dbAltersText = CORE_SCHEMA_ALTER_DDLS.join("\n");
for (const alterTable of ["client_sdks", "admin_settings", "admin_users"]) {
const alterRe = new RegExp(`ALTER TABLE\\s+${alterTable}\\s+ADD COLUMN IF NOT EXISTS`);
assert.match(dbAltersText, alterRe, `database.ts missing ALTER TABLE ${alterTable}`);
assert.match(serverIndexSrc, alterRe, `server/index.js missing ALTER TABLE ${alterTable}`);
}
// prisma/schema.prisma must explicitly document canonical runtime pg ownership
assert.match(prismaSchemaSrc, /REFERENCE SCHEMA ONLY/);
assert.match(prismaSchemaSrc, /core\/Database\/database\.ts/);
});
test("R2 PHASE-03 BUG-11: Flat batch items strip eventId/timestamp/clientId across both HTTP POST /api/events and Socket.IO batch_sync", async () => {
const client = await SDKManager.registerClientAsync({
clientId: "phase03_batch_strip",
name: "Batch Strip Client",
rateLimit: 200,
});
// 1. HTTP POST /api/events batch array
const httpBatchRes = await postEventsRoute(
new NextRequest("http://localhost:3000/api/events", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({
batch: [
{
eventId: "http-flat-batch-1",
timestamp: "2026-11-19T12:10:00.000Z",
clientId: client.clientId,
sensor: "temp",
value: 23.4,
},
{
eventId: "http-env-batch-2",
timestamp: "2026-11-19T12:10:01.000Z",
data: { sensor: "humidity", value: 60 },
},
],
}),
}),
);
assert.equal(httpBatchRes.status, 200);
const httpBatchJson = await httpBatchRes.json();
assert.equal(httpBatchJson.processed, 2);
const localSnap = DataBufferEngine.getLocalBufferSnapshot();
const flatEvt = localSnap.find((e) => e.eventId === "http-flat-batch-1");
const envEvt = localSnap.find((e) => e.eventId === "http-env-batch-2");
assert.ok(flatEvt);
assert.ok(envEvt);
assert.deepEqual(JSON.parse(flatEvt.payload), { sensor: "temp", value: 23.4 });
assert.deepEqual(JSON.parse(envEvt.payload), { sensor: "humidity", value: 60 });
// 2. Socket.IO batch_sync handler
const require = createRequire(import.meta.url);
const { setupSocketHandlers } = require("../server/socket/handlers") as {
setupSocketHandlers: (
io: unknown,
bufferQueue: DataEvent[],
worker: unknown,
options?: Record<string, unknown>,
) => unknown;
};
let connectionHandler: ((socket: unknown) => void) | null = null;
const fakeIo = {
use: () => undefined,
on: (evt: string, fn: (socket: unknown) => void) => {
if (evt === "connection") connectionHandler = fn;
},
};
const wsQueue: DataEvent[] = [];
setupSocketHandlers(fakeIo, wsQueue, {}, {
verifyClientInDb: async () => ({ allowed: true, rateLimit: 200 }),
});
assert.ok(connectionHandler);
const listeners = new Map<string, (...args: unknown[]) => Promise<void>>();
const fakeSocket = {
data: { clientId: "phase03_batch_strip", token: client.token, rateLimit: 200 },
join: () => undefined,
disconnect: () => undefined,
on: (name: string, fn: (...args: unknown[]) => Promise<void>) => {
listeners.set(name, fn);
},
};
(connectionHandler as (socket: unknown) => void)(fakeSocket);
const batchSyncHandler = listeners.get("batch_sync");
assert.ok(batchSyncHandler);
let wsAck: Record<string, unknown> | null = null;
await batchSyncHandler(
[
{
eventId: "ws-flat-batch-1",
timestamp: "2026-11-19T12:11:00.000Z",
clientId: "phase03_batch_strip",
sensor: "pressure",
value: 101.2,
},
],
(ack: Record<string, unknown>) => {
wsAck = ack;
},
);
assert.ok(wsAck);
assert.equal((wsAck as Record<string, unknown>).success, true);
assert.equal(wsQueue.length, 1);
assert.deepEqual(JSON.parse(wsQueue[0].payload), {
sensor: "pressure",
value: 101.2,
});
});
test("R2 PHASE-03 BUG-12: DataBufferEngine.getLocalBufferSnapshot returns exact state.buffer contents for getPendingBufferedSummary", async () => {
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
try {
// Seed 1 unflushed RAM event directly in buffer while recentEvents contains a DIFFERENT newer flushed item
await DataBufferEngine.pushAsync({
clientId: "phase03_buf_exact",
eventId: "real-unflushed-1",
timestamp: "2026-11-19T13:00:00.000Z",
payload: JSON.stringify({ state: "unflushed" }),
});
// Simulate a flushed event sitting at the front of recentEvents (which old getRecentEvents slice would wrongly return!)
g.__dataStackBufferState!.recentEvents.unshift({
clientId: "other_flushed_client",
eventId: "already-flushed-99",
timestamp: "2026-11-19T13:05:00.000Z",
payload: JSON.stringify({ state: "already-flushed" }),
});
const snap = DataBufferEngine.getLocalBufferSnapshot();
assert.equal(snap.length, 1);
assert.equal(snap[0].eventId, "real-unflushed-1");
const res = await getEventsRoute(
new NextRequest("http://localhost:3000/api/events?bufferedOnly=true&limit=10", {
headers: { cookie: adminCookieHeader },
}),
);
assert.equal(res.status, 200);
const json = await res.json();
assert.equal(json.events.length, 1);
assert.equal(json.events[0].eventId, "real-unflushed-1");
assert.equal(json.bufferedSummary.total, 1);
assert.equal(json.bufferedSummary.buckets[0].clientId, "phase03_buf_exact");
} finally {
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
}
});
+850
View File
@@ -0,0 +1,850 @@
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { NextRequest } from 'next/server';
import {
countMatchingBufferedEvents,
DEFAULT_EDITABLE_SETTINGS,
describeSettingsBridgeSync,
extractContentDispositionFilename,
formatEventTimeLabel,
formatSessionExpiryLabel,
getEventUtcDate,
getUtcDateRangeForPreset,
mapBridgeStatusToSocketUiState,
matchesSelectedClientFolder,
normalizeEditableSettings,
parseEventTimestamp,
resolvePublicEndpoints,
SETTINGS_LIMITS,
settingsToNumericDrafts,
shouldRedirectProfileUnauthorized,
summarizeHttpBatchResult,
validateEditableSettingsDraft,
validateSingleNumericSetting,
} from '../lib/vconUiHelpers';
import {
createAdminSessionToken,
getAdminSessionSecret,
} from '../core/auth/adminSession';
import {
createInitialAdmin,
resetAdminStateForTests,
} from '../core/auth/adminUsers';
import { DataBufferEngine } from '../core/data/buffer';
import { SDKManager } from '../core/sdk/sdkManager';
import { CSVStorageManager } from '../core/Storage/csvStorage';
import { GET as profileGet, PUT as profilePut } from '../app/api/auth/profile/route';
import { DELETE as deleteClientRoute } from '../app/api/clients/[id]/route';
import { GET as eventsGet, POST as eventsPost } from '../app/api/events/route';
import { POST as settingsPost } from '../app/api/settings/route';
test('BUG-D01: normalizeEditableSettings enforces exact /api/settings backend bounds', () => {
const high = normalizeEditableSettings({
maxEventsPerSec: 95000,
maxPayloadSizeKb: 10240,
batchFlushThreshold: 50000,
batchFlushIntervalMs: 60000,
fileRotationMb: 2048,
enableIdempotency: false,
autoPruneDays: 3650,
});
assert.equal(high.maxEventsPerSec, 95000);
assert.equal(high.maxPayloadSizeKb, 10240);
assert.equal(high.batchFlushThreshold, 50000);
assert.equal(high.batchFlushIntervalMs, 60000);
assert.equal(high.fileRotationMb, 2048);
assert.equal(high.enableIdempotency, false);
assert.equal(high.autoPruneDays, 3650);
// Zero on positive-min fields clamps to min (1 / 100), while autoPruneDays keeps 0 ("never prune")
const low = normalizeEditableSettings({
maxEventsPerSec: 0,
maxPayloadSizeKb: 0,
batchFlushThreshold: 0,
batchFlushIntervalMs: 10,
fileRotationMb: 0,
autoPruneDays: 0,
});
assert.equal(low.maxEventsPerSec, SETTINGS_LIMITS.maxEventsPerSec.min);
assert.equal(low.maxPayloadSizeKb, SETTINGS_LIMITS.maxPayloadSizeKb.min);
assert.equal(low.batchFlushThreshold, SETTINGS_LIMITS.batchFlushThreshold.min);
assert.equal(low.batchFlushIntervalMs, SETTINGS_LIMITS.batchFlushIntervalMs.min);
assert.equal(low.fileRotationMb, SETTINGS_LIMITS.fileRotationMb.min);
assert.equal(low.autoPruneDays, 0);
// Non-finite falls back to defaults
const fallback = normalizeEditableSettings({
maxEventsPerSec: Number.NaN,
autoPruneDays: Number.NaN,
});
assert.equal(fallback.maxEventsPerSec, DEFAULT_EDITABLE_SETTINGS.maxEventsPerSec);
assert.equal(fallback.autoPruneDays, DEFAULT_EDITABLE_SETTINGS.autoPruneDays);
});
test('BUG-D01 & BUG-D04: bridgeSync feedback and socket UI state accurately distinguish online, auth_error, and offline', () => {
assert.equal(describeSettingsBridgeSync(undefined).level, 'success');
assert.equal(describeSettingsBridgeSync({ attempted: true, ok: true }).level, 'success');
const authFailure = describeSettingsBridgeSync({
attempted: true,
ok: false,
status: 401,
error: 'Unauthorized internal bridge token',
});
assert.equal(authFailure.level, 'error');
assert.match(authFailure.title, /rejected sync/i);
const offlineFailure = describeSettingsBridgeSync({
attempted: true,
ok: false,
error: 'connect ECONNREFUSED 127.0.0.1:4000',
});
assert.equal(offlineFailure.level, 'warning');
assert.match(offlineFailure.title, /unreachable/i);
assert.equal(mapBridgeStatusToSocketUiState('online'), 'online');
assert.equal(mapBridgeStatusToSocketUiState('auth_error'), 'auth_error');
assert.equal(mapBridgeStatusToSocketUiState('offline'), 'offline');
assert.equal(mapBridgeStatusToSocketUiState('error'), 'offline');
assert.equal(mapBridgeStatusToSocketUiState(undefined), 'offline');
});
test('BUG-D03: getUtcDateRangeForPreset computes deterministic UTC ranges across day boundaries', () => {
const ref = new Date('2026-11-19T02:15:00.000Z');
assert.deepEqual(getUtcDateRangeForPreset('today', ref), {
from: '2026-11-19',
to: '2026-11-19',
});
assert.deepEqual(getUtcDateRangeForPreset('7d', ref), {
from: '2026-11-13',
to: '2026-11-19',
});
assert.deepEqual(getUtcDateRangeForPreset('30d', ref), {
from: '2026-10-21',
to: '2026-11-19',
});
assert.deepEqual(getUtcDateRangeForPreset('all', ref), {
from: '',
to: '',
});
});
test('BUG-D03 & BUG-D05: timestamp formatting handles ISO & epoch-ms strings cleanly without Invalid Date', () => {
const isoParsed = parseEventTimestamp('2026-11-18T12:34:56.000Z');
assert.ok(isoParsed instanceof Date);
assert.equal(isoParsed.toISOString(), '2026-11-18T12:34:56.000Z');
const epochParsed = parseEventTimestamp('1700000000000');
assert.ok(epochParsed instanceof Date);
assert.equal(epochParsed.getTime(), 1700000000000);
assert.notEqual(formatEventTimeLabel('1700000000000'), 'Invalid Date');
assert.notEqual(formatEventTimeLabel('1700000000000'), '1700000000000');
assert.equal(parseEventTimestamp('not-a-date'), null);
assert.equal(formatEventTimeLabel('not-a-date'), 'not-a-date');
assert.equal(formatEventTimeLabel(''), '—');
});
test('BUG-D03 & BUG-D06: matchesSelectedClientFolder and Content-Disposition parsing handle sanitized folders and quoted filenames', () => {
assert.equal(matchesSelectedClientFolder('sensor_01', 'all'), true);
assert.equal(matchesSelectedClientFolder('sensor_01', 'sensor_01'), true);
assert.equal(matchesSelectedClientFolder('client_dev', 'client@dev'), true);
assert.equal(matchesSelectedClientFolder('other_client', 'sensor_01'), false);
assert.equal(
extractContentDispositionFilename(
'attachment; filename="datastack_export_sensor_01_2026-11-18.json"',
'fallback.json'
),
'datastack_export_sensor_01_2026-11-18.json'
);
assert.equal(
extractContentDispositionFilename(null, 'fallback.csv'),
'fallback.csv'
);
});
test('FORENSIC BUG-09: parseEventTimestamp and getEventUtcDate handle integer and fractional epoch-seconds with backend parity', () => {
const intSec = parseEventTimestamp('1735689600');
assert.ok(intSec instanceof Date);
assert.equal(intSec.toISOString(), '2025-01-01T00:00:00.000Z');
assert.equal(getEventUtcDate('1735689600'), '2025-01-01');
assert.notEqual(formatEventTimeLabel('1735689600'), '1735689600');
const floatSec = parseEventTimestamp('1735689600.25');
assert.ok(floatSec instanceof Date);
assert.equal(floatSec.toISOString(), '2025-01-01T00:00:00.250Z');
assert.equal(getEventUtcDate('1735689600.25'), '2025-01-01');
assert.notEqual(formatEventTimeLabel('1735689600.25'), '1735689600.25');
const msVal = parseEventTimestamp('1735689600500');
assert.ok(msVal instanceof Date);
assert.equal(msVal.toISOString(), '2025-01-01T00:00:00.500Z');
});
test('FORENSIC BUG-07: POST /api/events ingests batch and events arrays cleanly with accepted count and Simulator never falsifies counts', async () => {
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'datastack-p5-bug07-'));
process.env.DATASTACK_STORAGE_DIR = tmpDir;
CSVStorageManager.baseStorageDir = tmpDir;
await DataBufferEngine.shutdown();
DataBufferEngine.setRedisClientForTests(null);
try {
const client = await SDKManager.registerClientAsync({
clientId: 'p5_sim_client',
name: 'Phase 05 Simulator Client',
rateLimit: 500,
});
const ts = new Date().toISOString();
// 1. Canonical { clientId, batch } payload (10 individual events)
const batchItems = Array.from({ length: 10 }, (_, idx) => ({
eventId: `p5_batch_${Date.now()}_${idx}`,
timestamp: ts,
data: { batch_index: idx + 1, sensor: 's1' },
}));
const reqBatch = new NextRequest('http://localhost:3000/api/events', {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ clientId: client.clientId, batch: batchItems }),
});
const resBatch = await eventsPost(reqBatch);
assert.equal(resBatch.status, 200);
const batchJson = await resBatch.json();
assert.equal(batchJson.success, true);
assert.equal(batchJson.accepted, 10);
assert.equal(batchJson.processed, 10);
assert.equal(batchJson.rejected, 0);
// 2. Alias { clientId, events } payload also ingests individual events (never 1 wrapper event)
const aliasItems = Array.from({ length: 4 }, (_, idx) => ({
eventId: `p5_alias_${Date.now()}_${idx}`,
timestamp: ts,
data: { alias_index: idx + 1 },
}));
const reqAlias = new NextRequest('http://localhost:3000/api/events', {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${client.token}`,
},
body: JSON.stringify({ clientId: client.clientId, events: aliasItems }),
});
const resAlias = await eventsPost(reqAlias);
assert.equal(resAlias.status, 200);
const aliasJson = await resAlias.json();
assert.equal(aliasJson.accepted, 4);
assert.equal(aliasJson.processed, 4);
// 3. summarizeHttpBatchResult never falsifies accepted=10 when server omits accepted/processed
const fakeSingleRes = summarizeHttpBatchResult(
true,
{ success: true, eventId: 'evt_wrapper', event: {} },
10,
);
assert.equal(fakeSingleRes.accepted, 0);
assert.equal(fakeSingleRes.rejected, 10);
assert.equal(fakeSingleRes.status, 'Rejected');
assert.equal(fakeSingleRes.level, 'error');
const partialRes = summarizeHttpBatchResult(
true,
{ success: true, accepted: 7, processed: 7, rejected: 3, duplicates: 0 },
10,
);
assert.equal(partialRes.accepted, 7);
assert.equal(partialRes.rejected, 3);
assert.equal(partialRes.status, 'Rejected');
assert.equal(partialRes.level, 'warning');
const okRes = summarizeHttpBatchResult(true, batchJson, 10);
assert.equal(okRes.accepted, 10);
assert.equal(okRes.rejected, 0);
assert.equal(okRes.status, 'Accepted');
assert.equal(okRes.level, 'success');
// Verify Simulator source sends canonical `batch:` and uses summarizeHttpBatchResult
const simSrc = await fs.readFile(
path.join(process.cwd(), 'app', 'vcon', 'simulator', 'page.tsx'),
'utf-8',
);
assert.match(simSrc, /batch:\s*batchPayload/);
assert.match(simSrc, /summarizeHttpBatchResult\(/);
assert.doesNotMatch(simSrc, /res\.ok\s*\?\s*count\s*:\s*0/);
} finally {
await DataBufferEngine.flush();
await DataBufferEngine.shutdown();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('FORENSIC BUG-08 & BUG-14: filtered buffered matching, post-flush zeroing, UTC-today attribution, and multi-client GET /api/events scan beyond 5 files', async () => {
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'datastack-p5-bug08-14-'));
process.env.DATASTACK_STORAGE_DIR = tmpDir;
CSVStorageManager.baseStorageDir = tmpDir;
process.env.INGESTION_INTERNAL_URL = 'http://127.0.0.1:1';
await DataBufferEngine.shutdown();
DataBufferEngine.setRedisClientForTests(null);
try {
const today = new Date().toISOString().slice(0, 10);
resetAdminStateForTests();
await createInitialAdmin('admin@datastack.dev', 'Phase05Pass123!');
const secret = getAdminSessionSecret()!;
const adminToken = await createAdminSessionToken(
'admin@datastack.dev',
secret,
Date.now() + 3600_000,
1,
);
// 1. Pure filtered buffer matcher prevents silent empty export downloads on non-matching client/date slices
const syntheticSummary = {
total: 8,
totalQueued: 8,
unattributedCount: 0,
truncated: false,
buckets: [
{ clientId: 'alpha_client', date: today, count: 3 },
{ clientId: 'alpha_client', date: '2025-01-01', count: 5 },
],
};
const otherClientMatch = countMatchingBufferedEvents(syntheticSummary, {
clientId: 'beta_client',
});
assert.equal(otherClientMatch.matchingCount, 0);
assert.equal(otherClientMatch.hasPotentialBuffered, false);
const outOfRangeMatch = countMatchingBufferedEvents(syntheticSummary, {
clientId: 'alpha_client',
fromDate: '2024-01-01',
toDate: '2024-12-31',
});
assert.equal(outOfRangeMatch.matchingCount, 0);
assert.equal(outOfRangeMatch.hasPotentialBuffered, false);
const todayOnlyMatch = countMatchingBufferedEvents(syntheticSummary, {
clientId: 'all',
fromDate: today,
toDate: today,
});
assert.equal(todayOnlyMatch.matchingCount, 3);
assert.equal(todayOnlyMatch.hasPotentialBuffered, true);
// 2. Write CSV partitions for 8 distinct clients (plus 6 extra dated files on client_01) to prove BUG-14 scan exceeds old 5-file cap
for (let d = 1; d <= 6; d += 1) {
await CSVStorageManager.appendBatch([
{
clientId: 'p5_client_01',
eventId: `evt_c1_old_${d}`,
timestamp: `2026-10-0${d}T10:00:00.000Z`,
payload: JSON.stringify({ n: d }),
},
]);
}
for (let i = 1; i <= 8; i += 1) {
const cid = `p5_client_0${i}`;
await CSVStorageManager.appendBatch([
{
clientId: cid,
eventId: `evt_${cid}_latest`,
timestamp: `${today}T12:00:0${i}.000Z`,
payload: JSON.stringify({ client: cid }),
},
]);
}
// 3. Push 2 UTC-today buffered events + 3 backdated buffered events into RAM buffer
await DataBufferEngine.pushBatchAsync([
{
clientId: 'p5_buf_client',
eventId: 'buf_today_1',
timestamp: `${today}T13:00:01.000Z`,
payload: '{"kind":"today"}',
},
{
clientId: 'p5_buf_client',
eventId: 'buf_today_2',
timestamp: `${today}T13:00:02.000Z`,
payload: '{"kind":"today"}',
},
{
clientId: 'p5_buf_client',
eventId: 'buf_old_1',
timestamp: '2025-02-01T08:00:00.000Z',
payload: '{"kind":"backdated"}',
},
{
clientId: 'p5_buf_client',
eventId: 'buf_old_2',
timestamp: '2025-02-01T08:00:01.000Z',
payload: '{"kind":"backdated"}',
},
{
clientId: 'p5_buf_client',
eventId: 'buf_old_3',
timestamp: '2025-02-01T08:00:02.000Z',
payload: '{"kind":"backdated"}',
},
]);
const reqAll = new NextRequest(
'http://localhost:3000/api/events?limit=25&includeBufferedSummary=true',
{
headers: { cookie: `datastack_admin_session=${adminToken}` },
},
);
const resAll = await eventsGet(reqAll);
assert.equal(resAll.status, 200);
const allJson = await resAll.json();
// All 8 disk clients must be represented in GET /api/events (past the old 5-file global cap)
const seenClients = new Set<string>(
(allJson.events as Array<{ clientId: string }>).map((e) => e.clientId),
);
for (let i = 1; i <= 8; i += 1) {
assert.ok(
seenClients.has(`p5_client_0${i}`),
`Expected p5_client_0${i} to be visible in GET /api/events multi-client scan`,
);
}
// bufferedSummary accurately separates 2 today events from 3 backdated events
assert.ok(allJson.bufferedSummary);
assert.equal(allJson.bufferedSummary.total, 5);
const todayBuffered = countMatchingBufferedEvents(allJson.bufferedSummary, {
clientId: 'all',
fromDate: today,
toDate: today,
});
assert.equal(todayBuffered.matchingCount, 2);
// After flush, GET /api/events?bufferedOnly=true drops to 0 (never double-counting flushed recentEvents)
await DataBufferEngine.flush();
const reqBufAfterFlush = new NextRequest(
'http://localhost:3000/api/events?bufferedOnly=true&limit=1',
{
headers: { cookie: `datastack_admin_session=${adminToken}` },
},
);
const resBufAfterFlush = await eventsGet(reqBufAfterFlush);
assert.equal(resBufAfterFlush.status, 200);
const bufAfterJson = await resBufAfterFlush.json();
assert.equal(bufAfterJson.bufferedSummary.total, 0);
assert.equal(bufAfterJson.bufferedSummary.buckets.length, 0);
// Verify ExportWorkspace & DashboardOverview refresh caches after export
const exportSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'ExportWorkspace.tsx'),
'utf-8',
);
assert.match(exportSrc, /countMatchingBufferedEvents\(/);
assert.match(exportSrc, /await refreshExportData\(\)/);
const dashSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'DashboardOverview.tsx'),
'utf-8',
);
assert.match(dashSrc, /includeBufferedSummary=true/);
assert.match(dashSrc, /await refresh\(\)/);
} finally {
await DataBufferEngine.flush();
await DataBufferEngine.shutdown();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('FORENSIC BUG-10: /api/auth/profile returns explicit SESSION_EXPIRED vs INVALID_PASSWORD codes and ProfileWorkspace formats full expiry date+time', async () => {
const prevDbUrl = process.env.DATABASE_URL;
const prevDemoFlag = process.env.DATASTACK_ALLOW_DEMO_ADMIN;
delete process.env.DATABASE_URL;
process.env.DATASTACK_ALLOW_DEMO_ADMIN = 'false';
try {
resetAdminStateForTests();
await createInitialAdmin('p6-admin@datastack.dev', 'Phase06Pass123!');
const secret = getAdminSessionSecret()!;
const validToken = await createAdminSessionToken(
'p6-admin@datastack.dev',
secret,
Date.now() + 30 * 86_400_000,
1,
);
// 1. Missing/expired session on GET & PUT returns 401 with code: SESSION_EXPIRED
const unauthGet = await profileGet(
new NextRequest('http://localhost:3000/api/auth/profile'),
);
assert.equal(unauthGet.status, 401);
const unauthGetJson = await unauthGet.json();
assert.equal(unauthGetJson.code, 'SESSION_EXPIRED');
const unauthPut = await profilePut(
new NextRequest('http://localhost:3000/api/auth/profile', {
method: 'PUT',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
email: 'p6-admin@datastack.dev',
currentPassword: 'Phase06Pass123!',
}),
}),
);
assert.equal(unauthPut.status, 401);
const unauthPutJson = await unauthPut.json();
assert.equal(unauthPutJson.code, 'SESSION_EXPIRED');
assert.equal(shouldRedirectProfileUnauthorized(401, unauthPutJson), true);
// 2. Wrong currentPassword on valid session returns 401 with code: INVALID_PASSWORD (no redirect)
const wrongPassRes = await profilePut(
new NextRequest('http://localhost:3000/api/auth/profile', {
method: 'PUT',
headers: {
'content-type': 'application/json',
cookie: `datastack_admin_session=${validToken}`,
},
body: JSON.stringify({
email: 'p6-admin@datastack.dev',
currentPassword: 'WrongPassword999!',
}),
}),
);
assert.equal(wrongPassRes.status, 401);
const wrongPassJson = await wrongPassRes.json();
assert.equal(wrongPassJson.code, 'INVALID_PASSWORD');
assert.equal(shouldRedirectProfileUnauthorized(401, wrongPassJson), false);
// 3. Session expiry label includes calendar date (not time-only)
const expiryLabel = formatSessionExpiryLabel('2026-12-19T14:30:00.000Z');
assert.match(expiryLabel, /\d{1,2}/);
assert.ok(
expiryLabel.length > 5 && /[A-Za-z]|\d{1,2}\/\d{1,2}/.test(expiryLabel),
`Expected date+time in session expiry label, got: ${expiryLabel}`,
);
assert.equal(formatSessionExpiryLabel(''), 'Active');
// 4. Dead code `response.status === 401 && !currentPassword` is removed from ProfileWorkspace
const profileUiSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'ProfileWorkspace.tsx'),
'utf-8',
);
assert.doesNotMatch(profileUiSrc, /response\.status === 401 && !currentPassword/);
assert.match(profileUiSrc, /shouldRedirectProfileUnauthorized\(/);
assert.match(profileUiSrc, /formatSessionExpiryLabel\(/);
} finally {
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
if (prevDemoFlag === undefined) delete process.env.DATASTACK_ALLOW_DEMO_ADMIN;
else process.env.DATASTACK_ALLOW_DEMO_ADMIN = prevDemoFlag;
resetAdminStateForTests();
}
});
test('FORENSIC BUG-11: ClientsTable exposes ?purgeFiles=true opt-in checkbox on delete and omits unused Secret key row', async () => {
const clientsTableSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'ClientsTable.tsx'),
'utf-8',
);
assert.match(clientsTableSrc, /\?purgeFiles=true/);
assert.match(clientsTableSrc, /Also purge stored CSV files for this client/);
assert.doesNotMatch(clientsTableSrc, /label="Secret key"/);
});
test('FORENSIC BUG-12: Settings numeric drafts preserve empty inputs and report explicit validation errors without silent clamping', async () => {
const validDrafts = settingsToNumericDrafts(DEFAULT_EDITABLE_SETTINGS);
const okResult = validateEditableSettingsDraft(validDrafts, true);
assert.equal(okResult.valid, true);
assert.deepEqual(okResult.values, DEFAULT_EDITABLE_SETTINGS);
// Empty string returns explicit required error (never resets to 0)
const emptyCheck = validateSingleNumericSetting('maxEventsPerSec', '');
assert.equal(emptyCheck.valid, false);
assert.match(emptyCheck.error, /required/i);
// Out-of-bounds value returns explicit range error (never silently clamped)
const invalidDrafts = {
...validDrafts,
batchFlushIntervalMs: '50',
fileRotationMb: '99999',
};
const badResult = validateEditableSettingsDraft(invalidDrafts, false);
assert.equal(badResult.valid, false);
assert.equal(badResult.firstInvalidKey, 'batchFlushIntervalMs');
assert.match(badResult.errors.batchFlushIntervalMs || '', /100/);
assert.match(badResult.errors.fileRotationMb || '', /2,048|2048/);
// autoPruneDays = "0" ("never prune") remains explicitly valid
const zeroPrune = validateSingleNumericSetting('autoPruneDays', '0');
assert.equal(zeroPrune.valid, true);
if (zeroPrune.valid) assert.equal(zeroPrune.value, 0);
const settingsUiSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'SettingsWorkspace.tsx'),
'utf-8',
);
assert.match(settingsUiSrc, /validateEditableSettingsDraft\(/);
assert.match(settingsUiSrc, /Socket port \(PORT_SOCKET\)/);
assert.match(settingsUiSrc, /noValidate/);
});
test('FORENSIC BUG-13: resolvePublicEndpoints unifies Settings, Simulator, and SDK Guide URLs across direct dev and port-80 localhost proxies', async () => {
// 1. Direct Next.js dev on :3000 maps ingestion to :serverPort
const directDev = resolvePublicEndpoints(
{
protocol: 'http:',
hostname: 'localhost',
host: 'localhost:3000',
port: '3000',
origin: 'http://localhost:3000',
},
4000,
);
assert.deepEqual(directDev, {
apiUrl: 'http://localhost:3000',
serverUrl: 'http://localhost:4000',
webSocketUrl: 'ws://localhost:4000',
});
// 2. Reverse-proxied localhost on port 80 stays same-origin (no bogus :4000)
const localProxy = resolvePublicEndpoints(
{
protocol: 'http:',
hostname: 'localhost',
host: 'localhost',
port: '',
origin: 'http://localhost',
},
4000,
);
assert.deepEqual(localProxy, {
apiUrl: 'http://localhost',
serverUrl: 'http://localhost',
webSocketUrl: 'ws://localhost',
});
// 3. HTTPS production domain uses wss:// and same-origin
const prodHttps = resolvePublicEndpoints(
{
protocol: 'https:',
hostname: 'data.example.com',
host: 'data.example.com',
port: '',
origin: 'https://data.example.com',
},
4000,
);
assert.deepEqual(prodHttps, {
apiUrl: 'https://data.example.com',
serverUrl: 'https://data.example.com',
webSocketUrl: 'wss://data.example.com',
});
// All 3 UI pages import and use resolvePublicEndpoints
for (const rel of [
path.join('components', 'sections', 'SettingsWorkspace.tsx'),
path.join('app', 'vcon', 'sdk', 'page.tsx'),
path.join('app', 'vcon', 'simulator', 'page.tsx'),
]) {
const src = await fs.readFile(path.join(process.cwd(), rel), 'utf-8');
assert.match(src, /resolvePublicEndpoints\(/, `${rel} must use resolvePublicEndpoints`);
}
});
test('R2 PHASE-03 BUG-03: POST /api/settings returns normalized bridgeSync contract and describeSettingsBridgeSync handles both synced and attempted/ok shapes', async () => {
const prevDbUrl = process.env.DATABASE_URL;
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
delete process.env.DATABASE_URL;
process.env.INGESTION_INTERNAL_URL = 'http://127.0.0.1:1';
try {
resetAdminStateForTests();
await createInitialAdmin('p3-settings@datastack.dev', 'Phase03Pass123!');
const secret = getAdminSessionSecret()!;
const adminToken = await createAdminSessionToken(
'p3-settings@datastack.dev',
secret,
Date.now() + 3600_000,
1,
);
const res = await settingsPost(
new NextRequest('http://localhost:3000/api/settings', {
method: 'POST',
headers: {
'content-type': 'application/json',
cookie: `datastack_admin_session=${adminToken}`,
},
body: JSON.stringify({ maxEventsPerSec: 150 }),
}),
);
assert.equal(res.status, 200);
const json = await res.json();
assert.ok(json.bridgeSync, 'POST /api/settings must return bridgeSync status');
assert.equal(json.bridgeSync.attempted, true);
assert.equal(json.bridgeSync.ok, false);
assert.equal(json.bridgeSync.synced, false);
// describeSettingsBridgeSync must produce a warning for offline bridgeSync from backend
const uiFeedback = describeSettingsBridgeSync(json.bridgeSync);
assert.equal(uiFeedback.level, 'warning');
assert.match(uiFeedback.title, /unreachable/i);
// Also accepts legacy { synced } shape directly without attempted/ok
assert.equal(describeSettingsBridgeSync({ synced: true }).level, 'success');
assert.equal(
describeSettingsBridgeSync({ synced: false, status: 401 }).level,
'error',
);
assert.equal(
describeSettingsBridgeSync({ synced: false, error: 'timeout' }).level,
'warning',
);
const settingsUiSrc = await fs.readFile(
path.join(process.cwd(), 'components', 'sections', 'SettingsWorkspace.tsx'),
'utf-8',
);
assert.match(settingsUiSrc, /data\.bridgeSync\?\.ok \?\? data\.bridgeSync\?\.synced/);
} finally {
await DataBufferEngine.shutdown();
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
resetAdminStateForTests();
}
});
test('R2 PHASE-03 BUG-04: DELETE /api/clients/[id]?purgeFiles=true returns exact integer purgedFiles count', async () => {
const prevDbUrl = process.env.DATABASE_URL;
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
const origBaseStorageDir = CSVStorageManager.baseStorageDir;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'datastack-r2p3-bug04-'));
delete process.env.DATABASE_URL;
process.env.DATASTACK_STORAGE_DIR = tmpDir;
CSVStorageManager.baseStorageDir = tmpDir;
try {
resetAdminStateForTests();
await createInitialAdmin('p3-purge@datastack.dev', 'Phase03Pass123!');
const secret = getAdminSessionSecret()!;
const adminToken = await createAdminSessionToken(
'p3-purge@datastack.dev',
secret,
Date.now() + 3600_000,
1,
);
const clientPurge = await SDKManager.registerClientAsync({
clientId: 'p3_purge_me',
name: 'Purge Me',
rateLimit: 100,
});
await CSVStorageManager.appendBatch([
{
clientId: clientPurge.clientId,
eventId: 'purge-e1',
timestamp: '2026-11-18T10:00:00.000Z',
payload: '{"a":1}',
},
{
clientId: clientPurge.clientId,
eventId: 'purge-e2',
timestamp: '2026-11-19T10:00:00.000Z',
payload: '{"a":2}',
},
]);
const delPurgeRes = await deleteClientRoute(
new NextRequest(
`http://localhost:3000/api/clients/${clientPurge.clientId}?purgeFiles=true`,
{
method: 'DELETE',
headers: { cookie: `datastack_admin_session=${adminToken}` },
},
),
{ params: Promise.resolve({ id: clientPurge.clientId }) },
);
assert.equal(delPurgeRes.status, 200);
const delPurgeJson = await delPurgeRes.json();
assert.equal(delPurgeJson.success, true);
assert.equal(delPurgeJson.purgedFiles, 2);
// Deleting without ?purgeFiles=true returns purgedFiles: 0 and retains CSV files
const clientKeep = await SDKManager.registerClientAsync({
clientId: 'p3_keep_files',
name: 'Keep Files',
rateLimit: 100,
});
await CSVStorageManager.appendBatch([
{
clientId: clientKeep.clientId,
eventId: 'keep-e1',
timestamp: '2026-11-19T10:00:00.000Z',
payload: '{"keep":true}',
},
]);
const delKeepRes = await deleteClientRoute(
new NextRequest(
`http://localhost:3000/api/clients/${clientKeep.clientId}`,
{
method: 'DELETE',
headers: { cookie: `datastack_admin_session=${adminToken}` },
},
),
{ params: Promise.resolve({ id: clientKeep.clientId }) },
);
assert.equal(delKeepRes.status, 200);
const delKeepJson = await delKeepRes.json();
assert.equal(delKeepJson.purgedFiles, 0);
const remaining = await CSVStorageManager.listAllFilesAsync();
assert.equal(
remaining.filter((f) => f.clientId === clientKeep.clientId).length,
1,
);
} finally {
await DataBufferEngine.shutdown();
CSVStorageManager.baseStorageDir = origBaseStorageDir;
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = prevDbUrl;
await fs.rm(tmpDir, { recursive: true, force: true });
resetAdminStateForTests();
}
});
test('R2 PHASE-03 BUG-15: /vcon/sdk clears previous client JWT synchronously on switch and guards config copy while loadingToken is true', async () => {
const sdkSrc = await fs.readFile(
path.join(process.cwd(), 'app', 'vcon', 'sdk', 'page.tsx'),
'utf-8',
);
assert.match(sdkSrc, /setSelectedClientToken\(''\);\s*\n\s*setLoadingToken\(true\);/);
const disabledMatches = sdkSrc.match(/disabled=\{loadingToken\}/g) || [];
assert.ok(
disabledMatches.length >= 3,
`Expected Copy Token and both Copy Config buttons to be guarded with disabled={loadingToken}, found ${disabledMatches.length}`,
);
});
+114
View File
@@ -0,0 +1,114 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
const ROOT = process.cwd();
test('BUG-E01 & BUG-E03: Canonical datastack and compat datastack-sdk packaging metadata, versions, and py.typed align', () => {
const pyproject = fs.readFileSync(
path.join(ROOT, 'sdk/python/datastack/pyproject.toml'),
'utf8',
);
const canonicalSetup = fs.readFileSync(
path.join(ROOT, 'sdk/python/datastack/setup.py'),
'utf8',
);
const compatSetup = fs.readFileSync(
path.join(ROOT, 'sdk-python/setup.py'),
'utf8',
);
const canonicalInit = fs.readFileSync(
path.join(ROOT, 'sdk/python/datastack/datastack/__init__.py'),
'utf8',
);
assert.match(canonicalInit, /__version__\s*=\s*["']2\.2\.0["']/);
assert.match(pyproject, /version\s*=\s*"2\.2\.0"/);
assert.match(canonicalSetup, /version="2\.2\.0"/);
assert.match(compatSetup, /version="2\.2\.0"/);
assert.equal(
fs.existsSync(path.join(ROOT, 'sdk/python/datastack/datastack/py.typed')),
true,
'PEP 561 py.typed marker must exist in datastack package',
);
assert.match(pyproject, /datastack\s*=\s*\["py\.typed"\]/);
assert.match(canonicalSetup, /package_data=\{"datastack": \["py\.typed"\]\}/);
for (const dep of [
'"python-socketio[client]>=5.8.0"',
'"requests>=2.28.0"',
'"websocket-client>=1.5.0"',
]) {
assert.ok(pyproject.includes(dep), `pyproject.toml missing ${dep}`);
assert.ok(canonicalSetup.includes(dep), `setup.py missing ${dep}`);
assert.ok(compatSetup.includes(dep), `sdk-python/setup.py missing ${dep}`);
}
});
test('BUG-E03 & BUG-E04: Root package.json engines and production start-all shutdown coordination are valid', () => {
const pkg = JSON.parse(
fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'),
) as {
engines?: { node?: string };
scripts?: Record<string, string>;
};
assert.equal(pkg.engines?.node, '>=20.0.0');
assert.equal(pkg.scripts?.start, 'node scripts/start-all.js');
assert.equal(pkg.scripts?.['start:web'], 'next start');
assert.equal(pkg.scripts?.['start:server'], 'node server/index.js');
const startAll = fs.readFileSync(
path.join(ROOT, 'scripts/start-all.js'),
'utf8',
);
assert.match(startAll, /markChildExited/);
assert.match(startAll, /exitedChildren >= 2/);
});
test('BUG-26: Legacy datastack-sdk declares datastack>=2.2.0 dependency and raises actionable ImportError', () => {
const compatSetup = fs.readFileSync(
path.join(ROOT, 'sdk-python/setup.py'),
'utf8',
);
const compatInit = fs.readFileSync(
path.join(ROOT, 'sdk-python/datastack_sdk/__init__.py'),
'utf8',
);
assert.ok(
compatSetup.includes('"datastack>=2.2.0"'),
'sdk-python/setup.py install_requires must include datastack>=2.2.0',
);
assert.match(
compatInit,
/raise ImportError\([\s\S]*datastack>=2\.2\.0[\s\S]*pip install \.\/sdk\/python\/datastack/,
'sdk-python/datastack_sdk/__init__.py must provide actionable install guidance if canonical datastack is missing',
);
});
test('BUG-27: start-all.js and dev-all.js avoid shell: true orphaning and allow >= 7000ms graceful shutdown flush', () => {
for (const relPath of ['scripts/start-all.js', 'scripts/dev-all.js']) {
const src = fs.readFileSync(path.join(ROOT, relPath), 'utf8');
assert.equal(
/shell:\s*true/.test(src),
false,
`${relPath} must NOT use shell: true (prevents orphaned child processes on SIGTERM)`,
);
assert.match(src, /shell:\s*false/, `${relPath} must explicitly set shell: false`);
assert.match(
src,
/spawn\(\s*process\.execPath/,
`${relPath} must spawn child services directly via process.execPath`,
);
assert.match(
src,
/GRACEFUL_SHUTDOWN_TIMEOUT_MS\s*=\s*7000/,
`${relPath} must wait at least 7000ms for graceful CSV/Redis shutdown flush`,
);
assert.match(src, /SIGKILL/, `${relPath} must escalate to SIGKILL after grace timeout`);
assert.match(src, /exitedChildren >= 2/, `${relPath} must wait for both child processes`);
}
});
+337
View File
@@ -9,6 +9,7 @@ Phase-05 Verification Tests (BUG-18):
offline disk spool replay against a local HTTP test server.
"""
import ast
import base64
import json
import os
@@ -32,9 +33,11 @@ import datastack # noqa: E402
import datastack_sdk # noqa: E402
from datastack import ( # noqa: E402
DataStack,
DataStackAuthError,
DataStackClient,
DataStackConfig,
DataStackConfigError,
DataStackConnectionError,
DataStackOfflineSpoolError,
DiskSpooler,
HeartbeatMonitor,
@@ -265,6 +268,340 @@ class Phase05PythonSdkTests(unittest.TestCase):
server.shutdown()
server.server_close()
def test_client_id_normalization_and_invalid_numeric_env(self) -> None:
tok_norm = _make_jwt({"clientId": "sensor_01", "sub": "sensor_01"})
# Case/punctuation variations matching server SDKManager.normalizeClientId() must succeed
client = DataStack(
server_url="http://127.0.0.1:49999",
api_url="http://127.0.0.1:39999",
token=tok_norm,
client_id="Sensor.01",
auto_connect=False,
)
self.assertEqual(client.client_id, "sensor_01")
client.disconnect()
# Invalid numeric env var must raise DataStackConfigError instead of raw ValueError
old_retries = os.environ.get("DATASTACK_MAX_RETRIES")
try:
os.environ["DATASTACK_MAX_RETRIES"] = "not-an-int"
with self.assertRaises(DataStackConfigError):
load_config()
finally:
if old_retries is None:
os.environ.pop("DATASTACK_MAX_RETRIES", None)
else:
os.environ["DATASTACK_MAX_RETRIES"] = old_retries
def test_ragged_csv_all_rejected_send_file_and_partial_rl_recovery(self) -> None:
tok = _make_jwt({"clientId": "csv_worker", "sub": "csv_worker"})
mode = {"behavior": "reject_oversized"}
seen_records: List[Dict[str, Any]] = []
class EdgeHandler(BaseHTTPRequestHandler):
def do_POST(self) -> None: # noqa: N802
length = int(self.headers.get("Content-Length", "0"))
raw = self.rfile.read(length).decode("utf-8")
parsed = json.loads(raw) if raw else {}
items = parsed.get("records") or parsed.get("batch") or []
seen_records.extend(items)
if mode["behavior"] == "reject_oversized":
resp = {
"success": True,
"processed": 0,
"rejected": len(items),
"duplicates": 0,
"oversizedEventIds": [it.get("eventId") for it in items],
}
status = 200
else:
# First round processes first item, rate-limits second; second round disconnects/429
if len(items) > 1:
resp = {
"success": True,
"processed": 1,
"rejected": 1,
"duplicates": 0,
"rateLimitedEventIds": [items[1]["eventId"]],
}
status = 200
else:
resp = {
"success": False,
"code": "RATE_LIMIT_EXCEEDED",
"error": "Still limited",
}
status = 429
body = json.dumps(resp).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, format: str, *args: Any) -> None:
return
server = ThreadingHTTPServer(("127.0.0.1", 0), EdgeHandler)
port = server.server_address[1]
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
with tempfile.TemporaryDirectory() as tmpdir:
# Ragged CSV with extra trailing column (used to crash sort_keys=True with None key)
ragged_csv = Path(tmpdir) / "ragged.csv"
ragged_csv.write_text(
"sensor,val\ns1,10,extra_trailing_col\n",
encoding="utf-8",
)
client = DataStack(
server_url="http://127.0.0.1:1",
api_url=f"http://127.0.0.1:{port}",
token=tok,
auto_connect=False,
request_timeout_sec=1.0,
max_retries=0,
retry_backoff_base_sec=0.1,
offline_spool_enabled=False,
)
res_file = client.send_file(ragged_csv)
self.assertFalse(res_file["success"])
self.assertEqual(res_file["processed"], 0)
self.assertEqual(res_file["rejected"], 1)
self.assertEqual(seen_records[0]["data"]["_extra"], ["extra_trailing_col"])
# Partial rate-limit where follow-up retry exhausts retries must preserve round-1 processed count
mode["behavior"] = "partial_rl"
spool_dir = Path(tmpdir) / "spool"
rl_client = DataStack(
server_url="http://127.0.0.1:1",
api_url=f"http://127.0.0.1:{port}",
token=tok,
auto_connect=False,
request_timeout_sec=1.0,
max_retries=1,
retry_backoff_base_sec=0.1,
offline_spool_dir=str(spool_dir),
)
rl_res = rl_client.send_batch(
[
{"eventId": "evt_ok_1", "data": {"n": 1}},
{"eventId": "evt_rl_2", "data": {"n": 2}},
]
)
self.assertTrue(rl_res["success"])
self.assertEqual(rl_res["processed"], 1)
self.assertEqual(rl_res["spooledCount"], 1)
self.assertEqual(rl_client.spooler.pending_count(), 1)
client.disconnect()
rl_client.disconnect()
finally:
server.shutdown()
server.server_close()
def test_heartbeat_stop_race_and_spooler_clean_ops(self) -> None:
hb = HeartbeatMonitor(interval_sec=5.0, recovery_callback=lambda: True)
hb.start()
hb.trigger_fallback("disconnect")
hb.stop("shutdown", join_timeout=0.5)
# Calling set_idle_connected after stop() must NOT resurrect IDLE state
hb.set_idle_connected()
self.assertEqual(hb.state, "STOPPED")
with tempfile.TemporaryDirectory() as tmpdir:
spool = DiskSpooler(spool_dir=tmpdir, max_size_mb=5, enabled=True)
spool.enqueue([{"eventId": "a"}, {"eventId": "b"}, {"eventId": "c"}])
self.assertEqual(spool.pending_count(), 3)
spool.ack_and_requeue_front(2, [{"eventId": "b_retry"}])
batch = spool.peek_batch(limit=10)
self.assertEqual([item["eventId"] for item in batch], ["b_retry", "c"])
self.assertFalse((Path(tmpdir) / "spool.jsonl.tmp").exists())
spool.ack_batch(2)
self.assertEqual(spool.pending_count(), 0)
def test_bug25_disconnects_leaked_socket_on_failed_ping_verify(self) -> None:
tok = _make_jwt({"clientId": "cli_bug25_sock"})
client = DataStack(
server_url="http://127.0.0.1:4000",
api_url="http://127.0.0.1:3000",
token=tok,
auto_connect=False,
enable_http_fallback=False,
offline_spool_enabled=False,
request_timeout_sec=0.5,
)
class FakeSocket:
def __init__(self) -> None:
self.connected = False
self.disconnect_calls = 0
def connect(self, *args: Any, **kwargs: Any) -> None:
self.connected = True
def disconnect(self) -> None:
self.disconnect_calls += 1
self.connected = False
fake_sio = FakeSocket()
client.sio = fake_sio
# Case 1: ping_verify returns False -> socket must be disconnected
client._verify_websocket_handshake = lambda timeout=None: False # type: ignore[method-assign]
self.assertFalse(client._connect_and_verify_websocket(wait_timeout=0.5))
self.assertFalse(client.is_connected)
self.assertFalse(fake_sio.connected)
self.assertEqual(fake_sio.disconnect_calls, 1)
# Case 2: ping_verify raises DataStackAuthError -> socket must be disconnected before re-raising
def raise_auth(timeout: Any = None) -> bool:
raise DataStackAuthError("CLIENT_REVOKED")
client._verify_websocket_handshake = raise_auth # type: ignore[method-assign]
with self.assertRaises(DataStackAuthError):
client._connect_and_verify_websocket(wait_timeout=0.5)
self.assertFalse(client.is_connected)
self.assertFalse(fake_sio.connected)
self.assertEqual(fake_sio.disconnect_calls, 2)
client.disconnect()
def test_bug25_strict_auto_connect_and_context_raise_connection_error(self) -> None:
tok = _make_jwt({"clientId": "cli_bug25_strict"})
# auto_connect=True with both fallback and offline spool disabled must raise DataStackConnectionError
with self.assertRaises(DataStackConnectionError):
DataStack(
server_url="http://127.0.0.1:1",
api_url="http://127.0.0.1:1",
token=tok,
auto_connect=True,
enable_http_fallback=False,
offline_spool_enabled=False,
request_timeout_sec=0.2,
)
# Context manager __enter__ with both fallback and offline spool disabled must also raise
strict_client = DataStack(
server_url="http://127.0.0.1:1",
api_url="http://127.0.0.1:1",
token=tok,
auto_connect=False,
enable_http_fallback=False,
offline_spool_enabled=False,
request_timeout_sec=0.2,
)
with self.assertRaises(DataStackConnectionError):
with strict_client:
pass
self.assertEqual(strict_client.heartbeat.state, "STOPPED")
def test_r2_phase03_bug11_flat_batch_and_file_strip_envelope_keys(self) -> None:
tok = _make_jwt({"clientId": "cli_bug11_strip"})
captured_batches: List[List[Dict[str, Any]]] = []
with tempfile.TemporaryDirectory() as tmpdir:
client = DataStack(
server_url="http://127.0.0.1:1",
api_url="http://127.0.0.1:1",
token=tok,
auto_connect=False,
offline_spool_enabled=False,
)
def fake_execute(**kwargs: Any) -> Dict[str, Any]:
ws_payload = kwargs.get("ws_payload")
if isinstance(ws_payload, list):
captured_batches.append(ws_payload)
return {
"success": True,
"processed": len(ws_payload),
"rejected": 0,
"transport": "HTTP",
}
return {"success": True, "processed": 0, "rejected": 0}
client._execute_with_retry_and_fallback = fake_execute # type: ignore[method-assign]
res_batch = client.send_batch(
[
{
"eventId": "py-flat-1",
"timestamp": "2026-11-19T12:00:00Z",
"clientId": "cli_bug11_strip",
"sensor": "temp",
"value": 24.5,
},
{
"eventId": "py-env-2",
"timestamp": "2026-11-19T12:00:01Z",
"data": {"sensor": "humidity", "value": 55},
},
]
)
self.assertTrue(res_batch["success"])
self.assertEqual(len(captured_batches), 1)
self.assertEqual(captured_batches[0][0]["eventId"], "py-flat-1")
self.assertEqual(
captured_batches[0][0]["data"],
{"sensor": "temp", "value": 24.5},
)
self.assertEqual(
captured_batches[0][1]["data"],
{"sensor": "humidity", "value": 55},
)
csv_path = Path(tmpdir) / "flat_records.csv"
csv_path.write_text(
"eventId,timestamp,clientId,sensor,reading\n"
"file-flat-1,2026-11-19T13:00:00Z,cli_bug11_strip,pressure,101.3\n",
encoding="utf-8",
)
res_file = client.send_file(csv_path)
self.assertTrue(res_file["success"])
self.assertEqual(len(captured_batches), 2)
self.assertEqual(captured_batches[1][0]["eventId"], "file-flat-1")
self.assertEqual(
captured_batches[1][0]["data"],
{"sensor": "pressure", "reading": "101.3"},
)
def test_r2_phase04_bug14_python38_ast_typing_compatibility(self) -> None:
pkg_dir = CANONICAL_PKG_ROOT / "datastack"
py_files = sorted(pkg_dir.glob("*.py"))
self.assertGreater(len(py_files), 0)
pep585_builtins = {"tuple", "list", "dict", "set", "frozenset", "type"}
for py_file in py_files:
src = py_file.read_text(encoding="utf-8")
tree = ast.parse(src, filename=str(py_file), feature_version=(3, 8))
has_future_annotations = any(
isinstance(node, ast.ImportFrom)
and node.module == "__future__"
and any(alias.name == "annotations" for alias in node.names)
for node in tree.body
)
if py_file.name == "client.py":
self.assertTrue(
has_future_annotations,
"client.py must declare `from __future__ import annotations`",
)
for node in ast.walk(tree):
if (
isinstance(node, ast.Subscript)
and isinstance(node.value, ast.Name)
and node.value.id in pep585_builtins
):
self.fail(
f"{py_file.name}:{node.lineno} uses Python 3.9+ PEP 585 generic `{node.value.id}[...]`"
)
if __name__ == "__main__":
unittest.main(verbosity=2)
+36
View File
@@ -45,6 +45,8 @@ test('BUG-25: PWA manifest, icons (192/512/maskable/apple-touch), and Service Wo
assert.match(swContent, /request\.method !== ['"]GET['"]/);
assert.match(swContent, /\/api\//);
assert.match(swContent, /\/socket\.io\//);
assert.match(swContent, /\/vcon\//);
assert.match(swContent, /CLEAR_AUTH_CACHE/);
assert.match(swContent, /new Response\(/);
const layoutContent = fs.readFileSync(path.join(ROOT, 'app/layout.tsx'), 'utf8');
@@ -79,3 +81,37 @@ test('BUG-17: Landing/Navbar/Footer/404 navigation uses root-relative anchors an
assert.match(notFoundContent, /href=["']\/["']/);
assert.match(notFoundContent, /href=["']\/vcon\/dashboard["']/);
});
test('FORENSIC BUG-15: RootLayout injects pre-hydration theme bootstrap script and AdminSetup supports theme restoration & toggle', () => {
const layoutContent = fs.readFileSync(path.join(ROOT, 'app/layout.tsx'), 'utf8');
assert.match(layoutContent, /<html[^>]*suppressHydrationWarning/);
assert.match(layoutContent, /datastack-theme/);
assert.match(layoutContent, /<head>/);
const setupContent = fs.readFileSync(
path.join(ROOT, 'components/sections/AdminSetup.tsx'),
'utf8',
);
assert.match(setupContent, /datastack-theme/);
assert.match(setupContent, /Toggle theme/);
assert.match(setupContent, /document\.documentElement\.classList\.toggle\(['"]light['"]/);
});
test('FORENSIC BUG-16: Service Worker serves uncached HTML offline fallback shell for /vcon/dashboard PWA navigations', () => {
const manifest = JSON.parse(
fs.readFileSync(path.join(ROOT, 'public/manifest.json'), 'utf8'),
);
assert.equal(manifest.start_url, '/vcon/dashboard');
const swContent = fs.readFileSync(path.join(ROOT, 'public/sw.js'), 'utf8');
assert.match(swContent, /buildOfflineShellResponse/);
assert.match(swContent, /request\.mode !== ['"]navigate['"]/);
assert.match(swContent, /text\/html; charset=utf-8/);
assert.match(swContent, /Cache-Control['"]:\s*['"]no-store['"]/);
// Ensure /vcon/* branch never calls cache.put on authenticated admin HTML
const vconBranch = swContent.match(
/if \(url\.pathname === '\/vcon' \|\| url\.pathname\.startsWith\('\/vcon\/'\)\) \{[\s\S]*?return;\s*\}/,
);
assert.ok(vconBranch, 'Expected dedicated /vcon/* navigation fallback branch in public/sw.js');
assert.doesNotMatch(vconBranch[0], /cache\.put/);
});
+288 -9
View File
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import { existsSync, mkdirSync, readFileSync, rmSync, utimesSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { resolve } from "node:path";
import test, { after } from "node:test";
import { NextRequest } from "next/server";
@@ -7,6 +8,8 @@ import { GET as getHealthRoute } from "../app/api/health/route";
import { SettingsManager } from "../core/adminSettings/settingsManager";
import { DataBufferEngine, type RedisBufferClient } from "../core/data/buffer";
const require = createRequire(import.meta.url);
const g = globalThis as typeof globalThis & {
__dataStackAdminSettings?: unknown;
__dataStackSettingsHydrated?: boolean;
@@ -19,6 +22,8 @@ const g = globalThis as typeof globalThis & {
const prevDbUrl = process.env.DATABASE_URL;
const prevRedisUrl = process.env.REDIS_URL;
const prevDisableListen = process.env.DATASTACK_DISABLE_LISTEN;
process.env.DATASTACK_DISABLE_LISTEN = "1";
after(async () => {
DataBufferEngine.setRedisClientForTests(undefined);
@@ -39,6 +44,9 @@ after(async () => {
if (prevRedisUrl === undefined) delete process.env.REDIS_URL;
else process.env.REDIS_URL = prevRedisUrl;
if (prevDisableListen === undefined) delete process.env.DATASTACK_DISABLE_LISTEN;
else process.env.DATASTACK_DISABLE_LISTEN = prevDisableListen;
const testDirs = [
resolve(process.cwd(), "storage", "data", "phase07_prune_guard"),
resolve(process.cwd(), "storage", "data", "phase07_shutdown_flush"),
@@ -87,15 +95,20 @@ test("DEPLOY-BLK-03 & 05: Dockerfiles guard recursive chown on /app/storage/data
assert.ok(lockJson.packages && Object.keys(lockJson.packages).length > 10);
});
test("DEPLOY-BLK-04: Both Nginx configs proxy = /health to socketio_upstream", () => {
for (const relPath of ["docker/coolify-nginx.conf", "nginx/default.conf"]) {
const conf = readFileSync(resolve(process.cwd(), relPath), "utf8");
assert.match(
conf,
/location\s*=\s*\/health\s*\{[\s\S]*?proxy_pass\s+http:\/\/socketio_upstream;/,
`${relPath} must route location = /health to socketio_upstream`,
);
}
test("DEPLOY-BLK-04: Both Nginx configs proxy = /health to ingestion upstream", () => {
const defaultConf = readFileSync(resolve(process.cwd(), "nginx/default.conf"), "utf8");
assert.match(
defaultConf,
/location\s*=\s*\/health\s*\{[\s\S]*?proxy_pass\s+http:\/\/socketio_upstream;/,
"nginx/default.conf must route location = /health to socketio_upstream",
);
const coolifyConf = readFileSync(resolve(process.cwd(), "docker/coolify-nginx.conf"), "utf8");
assert.match(
coolifyConf,
/location\s*=\s*\/health\s*\{[\s\S]*?proxy_pass\s+\$socketio_upstream;/,
"docker/coolify-nginx.conf must route location = /health to $socketio_upstream",
);
});
test("DEPLOY-BLK-03 docs: manifest.md and README.md align volume mount path (/app/storage) and include sdk/python/datastack", () => {
@@ -197,3 +210,269 @@ test("DEPLOY-BLK-06 shutdown & health: DataBufferEngine.flushAndShutdownOnSignal
assert.equal(json.status, "online");
assert.equal(json.bufferSize, 7);
});
test("BUG-28: nginx/default.conf aligns Socket.IO directives and server/index.js enforces configurable DATASTACK_CORS_ORIGINS", () => {
const nginxDefault = readFileSync(resolve(process.cwd(), "nginx", "default.conf"), "utf8");
assert.match(
nginxDefault,
/location \/socket\.io\/ \{[\s\S]*proxy_read_timeout 86400s;[\s\S]*proxy_send_timeout 86400s;[\s\S]*proxy_buffering off;/,
"nginx/default.conf must disable proxy_buffering and set 86400s read/send timeouts on /socket.io/",
);
const compose = readFileSync(resolve(process.cwd(), "docker", "docker-compose.yml"), "utf8");
assert.match(compose, /DATASTACK_CORS_ORIGINS:\s*"\$\{DATASTACK_CORS_ORIGINS:-\}"/);
const envExample = readFileSync(resolve(process.cwd(), ".env.example"), "utf8");
assert.match(envExample, /DATASTACK_CORS_ORIGINS=/);
const serverSrc = readFileSync(resolve(process.cwd(), "server", "index.js"), "utf8");
assert.equal(
serverSrc.includes("origin: '*'"),
false,
"server/index.js must not hardcode wildcard origin: '*'",
);
process.env.DATASTACK_DISABLE_LISTEN = "1";
const ingestionServer = require("../server/index.js") as {
isOriginAllowedByCors: (origin: string | undefined, raw?: string) => boolean;
corsOriginDelegate: (
origin: string | undefined,
cb: (err: Error | null, allow: boolean) => void,
) => void;
stopTimers: () => void;
};
try {
// Default / empty / wildcard allows any origin
assert.equal(ingestionServer.isOriginAllowedByCors("https://any.example.com", ""), true);
assert.equal(ingestionServer.isOriginAllowedByCors("https://any.example.com", "*"), true);
// Non-browser / SDK calls (no Origin header) remain allowed even with strict allowlist
const strictList = "https://console.example.com, https://admin.example.com/";
assert.equal(ingestionServer.isOriginAllowedByCors(undefined, strictList), true);
assert.equal(ingestionServer.isOriginAllowedByCors("", strictList), true);
// Allowed origins (with trailing slash normalization) pass
assert.equal(ingestionServer.isOriginAllowedByCors("https://console.example.com", strictList), true);
assert.equal(ingestionServer.isOriginAllowedByCors("https://console.example.com/", strictList), true);
assert.equal(ingestionServer.isOriginAllowedByCors("https://admin.example.com", strictList), true);
// Disallowed browser origins are rejected cleanly
assert.equal(ingestionServer.isOriginAllowedByCors("https://evil.example.com", strictList), false);
// Delegate callback respects live process.env.DATASTACK_CORS_ORIGINS without throwing
const prevCors = process.env.DATASTACK_CORS_ORIGINS;
try {
process.env.DATASTACK_CORS_ORIGINS = "https://allowed.datastack.dev";
let delegateErr: Error | null = null;
let delegateAllowed = true;
ingestionServer.corsOriginDelegate("https://blocked.datastack.dev", (err, allow) => {
delegateErr = err;
delegateAllowed = allow;
});
assert.equal(delegateErr, null);
assert.equal(delegateAllowed, false);
ingestionServer.corsOriginDelegate("https://allowed.datastack.dev", (err, allow) => {
delegateErr = err;
delegateAllowed = allow;
});
assert.equal(delegateErr, null);
assert.equal(delegateAllowed, true);
} finally {
if (prevCors === undefined) delete process.env.DATASTACK_CORS_ORIGINS;
else process.env.DATASTACK_CORS_ORIGINS = prevCors;
}
} finally {
ingestionServer.stopTimers();
}
});
test("R2 PHASE-04 BUG-16 & BUG-17: Nginx disables buffering on /api/export, next.config enforces ESLint during builds, and package.json prunes unused template deps", () => {
const nginxDefault = readFileSync(resolve(process.cwd(), "nginx/default.conf"), "utf8");
assert.match(
nginxDefault,
/location\s*=\s*\/api\/export\s*\{[\s\S]*?proxy_pass\s+http:\/\/nextjs_upstream;[\s\S]*?proxy_buffering\s+off;[\s\S]*?proxy_read_timeout\s+300s;/,
"nginx/default.conf must disable proxy_buffering and extend proxy_read_timeout on location = /api/export",
);
const coolifyConf = readFileSync(resolve(process.cwd(), "docker/coolify-nginx.conf"), "utf8");
assert.match(
coolifyConf,
/location\s*=\s*\/api\/export\s*\{[\s\S]*?proxy_pass\s+\$nextjs_upstream;[\s\S]*?proxy_buffering\s+off;[\s\S]*?proxy_read_timeout\s+300s;/,
"docker/coolify-nginx.conf must disable proxy_buffering and extend proxy_read_timeout on location = /api/export",
);
const nextConfig = readFileSync(resolve(process.cwd(), "next.config.ts"), "utf8");
assert.equal(
/ignoreDuringBuilds\s*:\s*true/.test(nextConfig),
false,
"next.config.ts must not ignore ESLint errors during builds",
);
const pkg = JSON.parse(readFileSync(resolve(process.cwd(), "package.json"), "utf8")) as {
name?: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};
assert.equal(pkg.name, "datastack");
assert.equal("@google/genai" in (pkg.dependencies ?? {}), false);
assert.equal("firebase-tools" in (pkg.devDependencies ?? {}), false);
});
test("DEPLOY-BUG-01 & 05: coolify-nginx.conf resolves Docker DNS dynamically and Coolify docs align Base Directory /", () => {
const coolifyConf = readFileSync(resolve(process.cwd(), "docker", "coolify-nginx.conf"), "utf8");
assert.match(coolifyConf, /resolver\s+127\.0\.0\.11\s+valid=10s\s+ipv6=off;/);
assert.match(coolifyConf, /set\s+\$nextjs_upstream\s+"http:\/\/web:3000";/);
assert.match(coolifyConf, /set\s+\$socketio_upstream\s+"http:\/\/ingestion:4000";/);
assert.equal(/\bupstream\s+\w+\s*\{/.test(coolifyConf), false, "coolify-nginx.conf must not define static upstream blocks");
const compose = readFileSync(resolve(process.cwd(), "docker", "docker-compose.yml"), "utf8");
const graceMatches = compose.match(/^\s+stop_grace_period:\s*20s/gm) ?? [];
assert.equal(graceMatches.length, 2, "web and ingestion must set stop_grace_period: 20s");
const installDoc = readFileSync(resolve(process.cwd(), "docs", "users", "installation.md"), "utf8");
const readmeDoc = readFileSync(resolve(process.cwd(), "README.md"), "utf8");
assert.match(installDoc, /\*\*Base Directory\*\*\s+to\s+`\/`/);
assert.match(readmeDoc, /\*\*Base Directory\*\*\s+to\s+`\/`\s+and\s+\*\*Docker Compose Location\*\*\s+to\s+`\/docker\/docker-compose\.yml`/);
});
test("DEPLOY-BUG-02, 03 & 04: Port 4000 memoizes schema DDLs, hydrates rotated JWT before listening, and drains buffers gracefully", async () => {
const serverSrc = readFileSync(resolve(process.cwd(), "server", "index.js"), "utf8");
assert.match(
serverSrc,
/async function startIngestionServer\(\)[\s\S]*syncSettingsFromDatabase\(\)[\s\S]*server\.listen\(PORT/,
"startIngestionServer must await bounded syncSettingsFromDatabase() before server.listen(PORT)",
);
assert.match(
serverSrc,
/async function gracefulShutdown\([\s\S]*await closeServerGracefully\(\);[\s\S]*Promise\.allSettled\(\[flushBuffer\(\),\s*flushPendingClientActivity\(\)\]\)/,
"gracefulShutdown must close HTTP/Socket.IO ingress before flushing buffers",
);
process.env.DATASTACK_DISABLE_LISTEN = "1";
const ingestionServer = require("../server/index.js") as {
runtimeConfig: { jwtSecret: string };
bufferQueue: Array<{ clientId: string; eventId: string; timestamp: string; payload: string }>;
setDbPoolForTests: (pool: unknown) => void;
setRedisClientForTests: (client: unknown) => void;
ensureDatabaseSchema: () => Promise<boolean>;
syncSettingsFromDatabase: () => Promise<boolean>;
gracefulShutdown: (signal: string, options?: { exit?: boolean }) => Promise<void>;
stopTimers: () => void;
};
const prevJwt = ingestionServer.runtimeConfig.jwtSecret;
let ddlCount = 0;
let selectCount = 0;
let failDdl = true;
let poolEnded = false;
const rotatedSecret = "rotated-production-jwt-secret-value-over-32-chars";
const mockPool = {
async query(sql: string) {
const normalized = sql.trim().toUpperCase();
if (normalized.startsWith("CREATE TABLE") || normalized.startsWith("ALTER TABLE")) {
ddlCount += 1;
if (failDdl) {
throw new Error("transient startup PG failure");
}
return { rowCount: 0, rows: [] };
}
if (normalized.includes("FROM ADMIN_SETTINGS")) {
selectCount += 1;
return {
rowCount: 1,
rows: [
{
max_events_per_sec: 250,
max_payload_size_kb: 128,
batch_flush_threshold: 100,
batch_flush_interval_ms: 2000,
file_rotation_mb: 50,
enable_idempotency: true,
auto_prune_days: 45,
jwt_secret: rotatedSecret,
},
],
};
}
return { rowCount: 0, rows: [] };
},
async end() {
poolEnded = true;
},
};
let redisQuitCalled = false;
const mockRedis = {
status: "ready",
async rpush() {
return 0;
},
async lpush() {
return 0;
},
async ltrim() {
return "OK";
},
async lrange() {
return [];
},
async llen() {
return 0;
},
async set() {
return "OK";
},
async eval() {
return 1;
},
async sismember() {
return 0;
},
async quit() {
redisQuitCalled = true;
return "OK";
},
};
try {
ingestionServer.setDbPoolForTests(mockPool);
// 1. Transient DDL error must return false and allow retry
const failedSync = await ingestionServer.syncSettingsFromDatabase();
assert.equal(failedSync, false);
assert.equal(ddlCount, 1);
// 2. Recovery runs 8 DDLs once and hydrates rotated jwt_secret; repeated calls do zero extra DDLs
failDdl = false;
ddlCount = 0;
assert.equal(await ingestionServer.syncSettingsFromDatabase(), true);
assert.equal(ddlCount, 8);
assert.equal(selectCount, 1);
assert.equal(ingestionServer.runtimeConfig.jwtSecret, rotatedSecret);
assert.equal(await ingestionServer.syncSettingsFromDatabase(), true);
assert.equal(await ingestionServer.syncSettingsFromDatabase(), true);
assert.equal(ddlCount, 8, "Repeated 3s sync ticks must NOT re-run CREATE/ALTER DDL statements");
assert.equal(selectCount, 3);
// 3. Graceful shutdown flushes buffered events, quits Redis, and closes PostgreSQL pool cleanly
ingestionServer.setRedisClientForTests(mockRedis);
ingestionServer.bufferQueue.push({
clientId: "phase07_shutdown_flush",
eventId: `evt-graceful-${Date.now()}`,
timestamp: new Date().toISOString(),
payload: JSON.stringify({ ok: true }),
});
await ingestionServer.gracefulShutdown("SIGTERM", { exit: false });
assert.equal(ingestionServer.bufferQueue.length, 0, "gracefulShutdown must drain RAM queue to CSV");
assert.equal(redisQuitCalled, true, "gracefulShutdown must invoke redis.quit()");
assert.equal(poolEnded, true, "gracefulShutdown must close PostgreSQL pool");
} finally {
ingestionServer.runtimeConfig.jwtSecret = prevJwt;
ingestionServer.setDbPoolForTests(null);
ingestionServer.setRedisClientForTests(undefined);
ingestionServer.stopTimers();
}
});