Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

11.1 KB
Raw Blame
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { test } from "node:test";
import {
	classifyProfile,
	emailHash,
	isoDate,
	type ProfileInput,
	sourceConsent,
} from "./profile";
import {
	contactUpdate,
	importContactUpdate,
	mergeConsent,
	profileFingerprint,
	type RemoteContact,
} from "./sync-policy";

const input = (): ProfileInput => ({
	source: {
		email: "[email protected]",
		created_at: "2026-01-01 12:00:00 UTC",
	},
	user: {
		id: "cap-user",
		email: "[email protected]",
		name: "Taylor",
		lastName: null,
		stripeSubscriptionStatus: null,
		thirdPartyStripeSubscriptionId: null,
		created_at: "2026-01-01T12:00:00.000Z",
		defaultOrgId: "personal",
	},
	memberships: [
		{
			userId: "cap-user",
			organizationId: "personal",
			ownerId: "cap-user",
			hasProSeat: false,
			tombstoneAt: null,
			workosConnectionId: null,
			createdAt: "2026-01-01T12:00:00.000Z",
		},
	],
	licenses: [],
	invited: false,
	sso: false,
	hasVideo: false,
	hasSharedVideo: false,
	now: new Date("2026-09-10T12:00:00.000Z"),
});
const remote = (): RemoteContact => ({
	id: "loops-id",
	email: "[email protected]",
	userId: "cap-user",
	subscribed: true,
	mailingLists: { tips: true },
});

test("greetings include a name only when a nonblank name is known", () => {
	const fixture = input();
	assert.equal(classifyProfile(fixture).capGreeting, "Hey Taylor,");
	fixture.source.first_name = "  Sam  ";
	assert.equal(classifyProfile(fixture).capGreeting, "Hey Sam,");
	fixture.user = undefined;
	for (const firstName of [undefined, "", "   ", "null", "NULL"]) {
		if (firstName === undefined) delete fixture.source.first_name;
		else fixture.source.first_name = firstName;
		assert.equal(classifyProfile(fixture).capGreeting, "Hey,");
	}
});

test("team greetings use a real first name and fall back to the team", () => {
	const fixture = input();
	assert.equal(classifyProfile(fixture).capTeamGreeting, "Hey Taylor,");
	fixture.user = undefined;
	for (const firstName of [undefined, "", "   ", "null"]) {
		if (firstName === undefined) delete fixture.source.first_name;
		else fixture.source.first_name = firstName;
		assert.equal(classifyProfile(fixture).capTeamGreeting, "Hey team,");
	}
	for (const roleName of [
		"Admin",
		"Operations",
		"Team",
		"post",
		"Owner",
		"USER",
		"Test",
		"account",
	]) {
		fixture.source.first_name = roleName;
		assert.equal(classifyProfile(fixture).capTeamGreeting, "Hey team,");
	}
});

test("case-study targeting leaves other contacts' sync fingerprints unchanged", () => {
	const profile = classifyProfile(input());
	const {
		capVerifiedAt: _verified,
		capImportedAt: _imported,
		capMultiSeatOwner: _owner,
		capTeamGreeting: _greeting,
		...legacy
	} = profile;
	assert.equal(
		profileFingerprint(profile),
		createHash("sha256").update(JSON.stringify(legacy)).digest("hex"),
	);
	assert.notEqual(
		profileFingerprint(profile),
		profileFingerprint({ ...profile, capMultiSeatOwner: true }),
	);
	assert.notEqual(
		profileFingerprint({ ...profile, capMultiSeatOwner: true }),
		profileFingerprint({
			...profile,
			capMultiSeatOwner: true,
			capTeamGreeting: "Hey team,",
		}),
	);
});

test("only organisation owners paying for more than one seat are multi-seat owners", () => {
	const value = input();
	if (!value.user) throw new Error("Missing fixture user");
	value.user.stripeSubscriptionStatus = "active";
	value.user.inviteQuota = 1;
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
	value.user.inviteQuota = 3;
	assert.equal(classifyProfile(value).capMultiSeatOwner, true);
	value.memberships[0].tombstoneAt = "2026-09-01T00:00:00.000Z";
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
	value.memberships[0].tombstoneAt = null;
	value.memberships[0].ownerId = "someone-else";
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
	value.memberships[0].ownerId = "cap-user";
	value.user.thirdPartyStripeSubscriptionId = "sub_team";
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
	value.user.thirdPartyStripeSubscriptionId = null;
	value.user.stripeSubscriptionStatus = "canceled";
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
	value.user.stripeSubscriptionStatus = null;
	value.user.inviteQuota = undefined;
	assert.equal(classifyProfile(value).capMultiSeatOwner, false);
});

test("an explicit source opt-out always wins over a positive custom flag", () => {
	assert.equal(
		sourceConsent({
			email: "[email protected]",
			unsubscribed_at: "2026-01-01",
			subscribed: "true",
		}),
		"unsubscribed",
	);
	assert.equal(
		sourceConsent({ email: "[email protected]", subscribed: "false" }),
		"unsubscribed",
	);
	assert.equal(
		sourceConsent({ email: "[email protected]", flag: "blocked" }),
		"suppressed",
	);
	assert.equal(
		sourceConsent({
			email: "[email protected]",
			unsubscribed_at: "2026-01-01",
			unsubscribed_reason: "hard_bounce",
		}),
		"suppressed",
	);
});

test("opt-out registry merges cannot resubscribe or clear a suppression", () => {
	for (const status of [
		"unknown",
		"subscribed",
		"unsubscribed",
		"suppressed",
	] as const) {
		assert.equal(mergeConsent("suppressed", status), "suppressed");
		assert.notEqual(mergeConsent("unsubscribed", status), "subscribed");
	}
});

test("email hashes ignore case and surrounding whitespace", () => {
	assert.equal(
		emailHash(" [email protected] "),
		emailHash("[email protected]"),
	);
});

test("historical Bento and MySQL dates become unambiguous API dates", () => {
	assert.equal(isoDate("2024-04-22 13:29:55 UTC"), "2024-04-22T13:29:55.000Z");
	assert.equal(isoDate("2024-04-22 13:29:55"), "2024-04-22T13:29:55.000Z");
	assert.throws(() => isoDate("not-a-date"));
});

test("verified independent free users are eligible but imported onboarding remains held", () => {
	const profile = classifyProfile(input());
	assert.equal(profile.capAudience, "free");
	assert.equal(profile.capPromotionalEligible, true);
	assert.equal(profile.capLifecycleEnabled, false);
	assert.equal(profile.capOnboardingEligible, false);
	assert.equal(profile.capLifecycleStage, "idle");
});

for (const status of ["active", "trialing", "complete", "paid", "past_due"]) {
	test(`${status} direct subscribers never enter the free promotion audience`, () => {
		const value = input();
		if (!value.user) throw new Error("Missing fixture user");
		value.user.stripeSubscriptionStatus = status;
		const profile = classifyProfile(value);
		assert.equal(profile.capAudience, "customer");
		assert.equal(profile.capPromotionalEligible, false);
	});
}

test("a desktop license prevents Pro upsell onboarding even with no cloud subscription", () => {
	const value = input();
	value.licenses = [
		{
			email: value.source.email,
			subscriptionActive: 1,
			nextRenewalDate: "2038-01-01T00:00:00.000Z",
			kind: "desktop",
		},
	];
	assert.equal(classifyProfile(value).capAudience, "customer");
	assert.equal(classifyProfile(value).capPlanName, "Cap Desktop");
	value.licenses[0].nextRenewalDate = "2025-01-01T00:00:00.000Z";
	assert.equal(classifyProfile(value).capAudience, "unknown");
});

for (const kind of [
	"membership",
	"invite",
	"sso",
	"latch",
	"origin",
	"seat",
] as const) {
	test(`${kind} teammate evidence excludes promotions`, () => {
		const value = input();
		if (!value.user) throw new Error("Missing fixture user");
		if (kind === "membership")
			value.memberships.push({
				...value.memberships[0],
				organizationId: "team",
				ownerId: "someone-else",
			});
		if (kind === "invite") value.invited = true;
		if (kind === "sso") value.sso = true;
		if (kind === "latch") value.teammateLatch = true;
		if (kind === "origin") value.user.marketingOrigin = "teammate";
		if (kind === "seat") value.user.thirdPartyStripeSubscriptionId = "sub_team";
		const profile = classifyProfile(value);
		assert.equal(profile.capAudience, "teammate");
		assert.equal(profile.capPromotionalEligible, false);
	});
}

test("unknown origin and historic customer tags fail closed", () => {
	const value = input();
	value.memberships = [];
	assert.equal(classifyProfile(value).capAudience, "unknown");
	const customer = input();
	customer.source.tags = "customer";
	assert.equal(classifyProfile(customer).capAudience, "unknown");
});

test("canceled cloud access can enter former audience but an active license overrides it", () => {
	const value = input();
	if (!value.user) throw new Error("Missing fixture user");
	value.user.stripeSubscriptionStatus = "canceled";
	assert.equal(classifyProfile(value).capAudience, "former");
	value.licenses = [
		{
			email: value.source.email,
			subscriptionActive: 1,
			nextRenewalDate: "2038-01-01T00:00:00.000Z",
			kind: "desktop",
		},
	];
	assert.equal(classifyProfile(value).capAudience, "customer");
});

test("sync never supplies subscribed true or replaces list subscriptions", () => {
	const profile = classifyProfile(input());
	for (const contact of [
		remote(),
		{ ...remote(), subscribed: false },
		{ ...remote(), mailingLists: {} },
	]) {
		const update = contactUpdate(profile, contact, "tips");
		assert.equal("subscribed" in update, false);
		assert.equal("mailingLists" in update, false);
		if (!contact.subscribed || !contact.mailingLists.tips)
			assert.equal(update.capPromotionalEligible, false);
	}
	profile.capConsent = "unsubscribed";
	assert.equal(contactUpdate(profile, remote(), "tips").subscribed, false);
});

test("existing teammate evidence and identities survive contact enrichment", () => {
	const profile = classifyProfile(input());
	const update = contactUpdate(
		profile,
		{ ...remote(), capTeammate: true },
		"tips",
	);
	assert.equal(update.capAudience, "teammate");
	assert.equal(update.capPromotionalEligible, false);
	assert.throws(() =>
		contactUpdate(profile, { ...remote(), userId: "another-cap-user" }, "tips"),
	);
	profile.userId = `bento:${emailHash(profile.email)}`;
	assert.equal(contactUpdate(profile, remote(), "tips").userId, "cap-user");
});

test("migration preserves missing, removed and unsubscribed list preferences", () => {
	const profile = {
		...classifyProfile(input()),
		capOnboardingEligible: true,
		capLifecycleEnabled: true,
		capLifecycleStage: "free",
	};
	const update = importContactUpdate(profile, remote(), "tips");
	assert.equal("mailingLists" in update, false);
	assert.equal(update.capPromotionalEligible, true);
	assert.equal(update.capLifecycleEnabled, true);
	assert.equal("subscribed" in update, false);
	for (const contact of [
		{ ...remote(), subscribed: false },
		{ ...remote(), mailingLists: {} },
		{ ...remote(), mailingLists: { other: true } },
		{ ...remote(), mailingLists: { tips: false } },
	]) {
		const held = importContactUpdate(profile, contact, "tips");
		assert.equal("mailingLists" in held, false);
		assert.equal("subscribed" in held, false);
		assert.equal(held.capPromotionalEligible, false);
		assert.equal(held.capOnboardingEligible, false);
		assert.equal(held.capLifecycleEnabled, false);
		assert.equal(held.capLifecycleStage, "idle");
	}
});

test("verification timestamps do not churn the sync fingerprint", () => {
	const profile = classifyProfile(input());
	assert.equal(
		profileFingerprint(profile),
		profileFingerprint({
			...profile,
			capVerifiedAt: "later",
			capImportedAt: "later",
		}),
	);
	assert.notEqual(
		profileFingerprint(profile),
		profileFingerprint({ ...profile, capTeammate: true }),
	);
});