bidstage/test/launch-notify.test.ts
caelum0x 9b66375f8f fix: address review findings on reversal auditability and session persistence
HIGH — the reversal clamp no longer erases the alerting/audit trail: a
reversal that applies nothing (already fully reversed, or clamped to zero)
now records a 'clamped_reversal' adjustment row with zero applied amount
and the provider-claimed cumulative, and partial clamps store the
provider-claimed cumulative on the refund row, so provider-side
over-refunds are queryable instead of silently absorbed (migration 0024
extends the adjustment_type/amount CHECKs accordingly).

MEDIUM — persistProviderCheckoutSession now throws on a zero-row UPDATE
instead of returning nulls, so a missing checkout intent fails the request
rather than handing the buyer an unsettleable provider session.

LOW — launch-notify origin check uses the configured appUrl (same policy
as checkout); repository URLs are stored WHATWG-normalized (parsed.href);
the launch_notifications email CHECK now mirrors the application regex.

Integration: +1 over-refund audit scenario; refund-after-dispute now
asserts the no-op refund id is recorded (8/8 green vs real Postgres 16).
2026-09-05 17:29:18 +03:00

54 lines
2.5 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
isLaunchNotifySource,
normalizeLaunchNotifyEmail,
normalizeLaunchNotifyRepositoryUrl,
} from "../lib/launch-notify";
test("normalizeLaunchNotifyEmail trims, lowercases, and validates shape", () => {
assert.equal(normalizeLaunchNotifyEmail(" Founder@Example.COM "), "founder@example.com");
assert.equal(normalizeLaunchNotifyEmail("a.b+tag@sub.domain.dev"), "a.b+tag@sub.domain.dev");
});
test("normalizeLaunchNotifyEmail rejects invalid addresses", () => {
for (const bad of ["", " ", "plainaddress", "@no-local.dev", "user@", "user@@double.dev", "user@nodot", `a${"x".repeat(255)}@example.com`]) {
assert.throws(() => normalizeLaunchNotifyEmail(bad), /email/i, `expected rejection: ${JSON.stringify(bad)}`);
}
assert.throws(() => normalizeLaunchNotifyEmail(42), /email/i);
assert.throws(() => normalizeLaunchNotifyEmail(undefined), /email/i);
});
test("normalizeLaunchNotifyRepositoryUrl accepts http(s) URLs and null", () => {
assert.equal(
normalizeLaunchNotifyRepositoryUrl("https://github.com/acme/widget"),
"https://github.com/acme/widget",
);
assert.equal(normalizeLaunchNotifyRepositoryUrl(" https://gitlab.com/a/b "), "https://gitlab.com/a/b");
// WHATWG-normalized: scheme and host lowercase, path casing preserved.
assert.equal(
normalizeLaunchNotifyRepositoryUrl("HTTPS://GitHub.com/Acme/Widget"),
"https://github.com/Acme/Widget",
);
assert.equal(normalizeLaunchNotifyRepositoryUrl(null), null);
assert.equal(normalizeLaunchNotifyRepositoryUrl(undefined), null);
assert.equal(normalizeLaunchNotifyRepositoryUrl(""), null);
});
test("normalizeLaunchNotifyRepositoryUrl rejects non-web schemes and oversized values", () => {
assert.throws(() => normalizeLaunchNotifyRepositoryUrl("javascript:alert(1)"), /repository/i);
assert.throws(() => normalizeLaunchNotifyRepositoryUrl("ftp://example.com/x"), /repository/i);
assert.throws(() => normalizeLaunchNotifyRepositoryUrl("not a url"), /repository/i);
assert.throws(() => normalizeLaunchNotifyRepositoryUrl(`https://example.com/${"x".repeat(600)}`), /repository/i);
assert.throws(() => normalizeLaunchNotifyRepositoryUrl(42), /repository/i);
});
test("isLaunchNotifySource accepts only known sources", () => {
assert.equal(isLaunchNotifySource("checkout_disabled"), true);
assert.equal(isLaunchNotifySource("landing"), true);
assert.equal(isLaunchNotifySource("spam"), false);
assert.equal(isLaunchNotifySource(""), false);
assert.equal(isLaunchNotifySource(42), false);
});