Development #24

Merged
kyle merged 35 commits from development into main 2026-08-28 10:40:10 -05:00
2 changed files with 52 additions and 2 deletions
Showing only changes of commit f5b49d49ee - Show all commits
@@ -0,0 +1,47 @@
import { describe, it, expect, vi } from "vitest";
import OverrideableBuilder from "supertokens-js-override";
import { withPasswordlessTelemetry } from "./auth-events.server";
vi.mock("@/lib/pocketbase/client", () => ({
pbAdmin: {
authPromise: Promise.resolve(),
getPlayerByAuthId: async () => undefined,
createActivity: async () => undefined,
},
}));
const makeOriginalImpl = () => ({
createCode: async (input: any) => ({ status: "OK", input }),
consumeCode: async (input: any) => ({ status: "OK", user: { id: "u1" }, input }),
listCodes: async () => [],
});
describe("withPasswordlessTelemetry", () => {
it("composes with an inner override that spreads the proxy (twilio recipe shape)", async () => {
const inner = (originalImplementation: any) => ({
...originalImplementation,
consumeCode: async (input: any) => originalImplementation.consumeCode(input),
});
const builder = new OverrideableBuilder(makeOriginalImpl() as any);
builder.override(withPasswordlessTelemetry(inner) as any);
const impl = builder.build() as any;
await expect(impl.createCode({ phoneNumber: "+15555550100" })).resolves.toMatchObject({
status: "OK",
});
await expect(impl.consumeCode({ preAuthSessionId: "x" })).resolves.toMatchObject({
status: "OK",
});
});
it("works without an inner override", async () => {
const builder = new OverrideableBuilder(makeOriginalImpl() as any);
builder.override(withPasswordlessTelemetry() as any);
const impl = builder.build() as any;
await expect(impl.createCode({ phoneNumber: "+15555550100" })).resolves.toMatchObject({
status: "OK",
});
});
});
+5 -2
View File
@@ -60,11 +60,14 @@ export const withPasswordlessTelemetry =
(originalImplementation: any) => { (originalImplementation: any) => {
const base = existing ? existing(originalImplementation) : originalImplementation; const base = existing ? existing(originalImplementation) : originalImplementation;
const callBase = (name: string, input: any) =>
(base[name] ?? originalImplementation[name]).call(originalImplementation, input);
return { return {
...base, ...base,
createCode: async (input: any) => { createCode: async (input: any) => {
try { try {
const response = await base.createCode(input); const response = await callBase("createCode", input);
recordAuthEvent("auth.otp_sent", { recordAuthEvent("auth.otp_sent", {
phone: input.phoneNumber, phone: input.phoneNumber,
success: response.status === "OK", success: response.status === "OK",
@@ -82,7 +85,7 @@ export const withPasswordlessTelemetry =
}, },
consumeCode: async (input: any) => { consumeCode: async (input: any) => {
try { try {
const response = await base.consumeCode(input); const response = await callBase("consumeCode", input);
if (response.status === "OK") { if (response.status === "OK") {
recordAuthEvent("auth.otp_consumed", { recordAuthEvent("auth.otp_consumed", {
authUserId: response.user?.id, authUserId: response.user?.id,