Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,175 @@
import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
import { Hono } from "hono";
import type { ApiEnv } from "../api-types.js";
import { registerDevicesRoutes } from "./devices-routes.js";
import { setApiRuntimeConfigForTests, resetApiRuntimeConfigForTests } from "../config.js";
const TEST_TOKEN = "test-service-token";
let mockStore: Map<string, { id: string; userId: string; tenantId: string | null; platform: string; token: string; pushProvider: string; appVersion: string | null; createdAt: Date; lastSeenAt: Date; revokedAt: Date | null }>;
vi.mock("@toolctrl/db/device-tokens.server", () => ({
parseDevicePlatform: (value: unknown) => {
return value === "ios" || value === "android" || value === "web" ? value : null;
},
parsePushProvider: (value: unknown) => {
return value === "fcm" || value === "apns" || value === "webpush" ? value : null;
},
async registerDeviceToken(input: { userId: string; tenantId: string | null; platform: "ios" | "android" | "web"; token: string; pushProvider?: "fcm" | "apns" | "webpush"; appVersion?: string | null }) {
const provider = input.pushProvider ?? (input.platform === "ios" ? "apns" : input.platform === "android" ? "fcm" : "webpush");
const now = new Date();
const existing = mockStore.get(input.token);
if (existing) {
existing.userId = input.userId;
existing.tenantId = input.tenantId;
existing.platform = input.platform;
existing.pushProvider = provider;
existing.appVersion = input.appVersion ?? null;
existing.lastSeenAt = now;
existing.revokedAt = null;
return existing;
}
const row = {
id: `dt-${mockStore.size + 1}`,
userId: input.userId,
tenantId: input.tenantId,
platform: input.platform,
token: input.token,
pushProvider: provider,
appVersion: input.appVersion ?? null,
createdAt: now,
lastSeenAt: now,
revokedAt: null,
};
mockStore.set(input.token, row);
return row;
},
async revokeDeviceToken(token: string) {
const row = mockStore.get(token);
if (!row) return null;
row.revokedAt = new Date();
return row;
},
}));
function buildApp() {
const app = new Hono<ApiEnv>();
registerDevicesRoutes(app);
return app;
}
beforeEach(() => {
mockStore = new Map();
setApiRuntimeConfigForTests({
apiAccessToken: TEST_TOKEN,
jwtSecret: "jwt",
routing: { appBaseDomain: "example.com", webAppHost: "app.example.com", adminAppHost: "admin.example.com", apiHost: "api.example.com", protocol: "https" },
apiKeys: { allowLegacyPlaintextFallback: false },
} as never);
});
afterEach(() => {
resetApiRuntimeConfigForTests();
});
function postRegister(app: ReturnType<typeof buildApp>, body: unknown, auth: string | null = `Bearer ${TEST_TOKEN}`) {
return app.request("/v1/devices/register", {
method: "POST",
headers: {
"content-type": "application/json",
...(auth ? { authorization: auth } : {}),
},
body: JSON.stringify(body),
});
}
describe("POST /v1/devices/register", () => {
it("rejects requests without service credentials", async () => {
const app = buildApp();
const response = await postRegister(app, {}, null);
expect(response.status).toBe(401);
});
it("rejects non-object bodies", async () => {
const app = buildApp();
const response = await app.request("/v1/devices/register", {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${TEST_TOKEN}` },
body: "not-json",
});
expect(response.status).toBe(400);
});
it("rejects missing userId", async () => {
const app = buildApp();
const response = await postRegister(app, { platform: "ios", token: "t" });
expect(response.status).toBe(400);
const json = (await response.json()) as { error: { params: { field: string } } };
expect(json.error.params.field).toBe("userId");
});
it("rejects unknown platform", async () => {
const app = buildApp();
const response = await postRegister(app, { userId: "u1", platform: "wear", token: "t" });
expect(response.status).toBe(400);
});
it("rejects missing token", async () => {
const app = buildApp();
const response = await postRegister(app, { userId: "u1", platform: "ios" });
expect(response.status).toBe(400);
const json = (await response.json()) as { error: { params: { field: string } } };
expect(json.error.params.field).toBe("token");
});
it("upserts a valid registration and defaults the push provider", async () => {
const app = buildApp();
const response = await postRegister(app, {
userId: "u-1",
tenantId: "t-1",
platform: "ios",
token: "tok-1",
appVersion: "1.2.3",
});
expect(response.status).toBe(200);
const json = (await response.json()) as { success: boolean; data: { pushProvider: string; platform: string; appVersion: string | null } };
expect(json.success).toBe(true);
expect(json.data.platform).toBe("ios");
expect(json.data.pushProvider).toBe("apns");
expect(json.data.appVersion).toBe("1.2.3");
expect(mockStore.size).toBe(1);
});
it("is idempotent: re-registering the same token bumps lastSeenAt without duplicating", async () => {
const app = buildApp();
await postRegister(app, { userId: "u-1", tenantId: null, platform: "android", token: "tok-x" });
const second = await postRegister(app, { userId: "u-1", tenantId: null, platform: "android", token: "tok-x" });
expect(second.status).toBe(200);
expect(mockStore.size).toBe(1);
});
});
describe("POST /v1/devices/revoke", () => {
it("revokes a known token", async () => {
const app = buildApp();
await postRegister(app, { userId: "u-1", platform: "ios", token: "tok-a" });
const response = await app.request("/v1/devices/revoke", {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${TEST_TOKEN}` },
body: JSON.stringify({ token: "tok-a" }),
});
expect(response.status).toBe(200);
const json = (await response.json()) as { data: { revokedAt: string | null } };
expect(json.data.revokedAt).not.toBeNull();
});
it("returns 404 for unknown tokens", async () => {
const app = buildApp();
const response = await app.request("/v1/devices/revoke", {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${TEST_TOKEN}` },
body: JSON.stringify({ token: "missing" }),
});
expect(response.status).toBe(404);
});
});