🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
import { beforeEach, describe, expect, it, vi } from "vitest";
const state = vi.hoisted(() => ({
site: null as Record<string, unknown> | null,
updateError: null as Error | null,
deleteError: null as Error | null,
updates: [] as Record<string, unknown>[],
deletes: 0,
isCloud: false,
}));
const mocks = vi.hoisted(() => ({
clickhouseCommand: vi.fn(),
invalidate: vi.fn(),
getConfig: vi.fn(),
}));
vi.mock("../../db/postgres/postgres.js", () => ({
db: {
query: {
sites: {
findFirst: vi.fn(async () => state.site),
},
},
update: vi.fn(() => ({
set: (data: Record<string, unknown>) => ({
where: async () => {
if (state.updateError) throw state.updateError;
state.updates.push(data);
},
}),
})),
delete: vi.fn(() => ({
where: async () => {
if (state.deleteError) throw state.deleteError;
state.deletes += 1;
},
})),
// Rolls back the counted deletes when the callback throws.
transaction: vi.fn(async (fn: (tx: unknown) => Promise<unknown>) => {
const { db } = await import("../../db/postgres/postgres.js");
const deletesBefore = state.deletes;
try {
return await fn(db);
} catch (error) {
state.deletes = deletesBefore;
throw error;
}
}),
},
}));
vi.mock("../../db/clickhouse/clickhouse.js", () => ({
clickhouse: { command: mocks.clickhouseCommand },
}));
vi.mock("../../lib/siteConfig.js", () => ({
siteConfig: { invalidate: mocks.invalidate, getConfig: mocks.getConfig },
}));
vi.mock("../../api/stripe/getSubscription.js", () => ({
getSubscriptionInner: vi.fn(),
}));
vi.mock("../../lib/const.js", async importOriginal => ({
...(await importOriginal<typeof import("../../lib/const.js")>()),
get IS_CLOUD() {
return state.isCloud;
},
}));
import { getSubscriptionInner } from "../../api/stripe/getSubscription.js";
import { siteConfigurationLifecycle } from "./siteConfigurationLifecycle.js";
function makeSite() {
return {
siteId: 1,
id: "abcdef123456",
type: null,
domain: "example.com",
organizationId: "org_1",
};
}
beforeEach(() => {
vi.clearAllMocks();
state.site = makeSite();
state.updateError = null;
state.deleteError = null;
state.updates.length = 0;
state.deletes = 0;
state.isCloud = false;
mocks.clickhouseCommand.mockResolvedValue(undefined);
mocks.getConfig.mockResolvedValue({ siteId: 1, id: "abcdef123456", name: "Renamed" });
});
describe("siteConfigurationLifecycle", () => {
it.each([true, false])("persists URL fragment tracking set to %s and invalidates the cache", async enabled => {
await siteConfigurationLifecycle.update(1, { trackUrlFragments: enabled });
expect(state.updates).toEqual([expect.objectContaining({ trackUrlFragments: enabled })]);
expect(mocks.invalidate).toHaveBeenCalledWith(state.site);
});
it("updates persistence once, invalidates the Site, and reloads its configuration", async () => {
const result = await siteConfigurationLifecycle.update(1, { name: "Renamed" });
expect(state.updates).toHaveLength(1);
expect(state.updates[0]).toMatchObject({ name: "Renamed" });
expect(mocks.invalidate).toHaveBeenCalledOnce();
expect(mocks.invalidate).toHaveBeenCalledWith(state.site);
expect(mocks.getConfig).toHaveBeenCalledWith(1);
expect(result).toMatchObject({ name: "Renamed" });
});
it("propagates persistence failures without invalidating the cache", async () => {
state.updateError = new Error("postgres unavailable");
await expect(siteConfigurationLifecycle.update(1, { name: "Renamed" })).rejects.toThrow("postgres unavailable");
expect(mocks.invalidate).not.toHaveBeenCalled();
expect(mocks.getConfig).not.toHaveBeenCalled();
});
describe("Standard feature entitlement on cloud", () => {
beforeEach(() => {
state.isCloud = true;
});
it("refuses to turn on a Standard feature without an active subscription", async () => {
vi.mocked(getSubscriptionInner).mockResolvedValue({ status: "free", includesReplay: false } as any);
await expect(siteConfigurationLifecycle.update(1, { webVitals: true })).rejects.toMatchObject({
code: "standard_features_not_entitled",
statusCode: 403,
});
expect(state.updates).toEqual([]);
});
it.each(["active", "trialing"])("turns a Standard feature on for a %s subscription", async status => {
vi.mocked(getSubscriptionInner).mockResolvedValue({ status, includesReplay: false } as any);
await siteConfigurationLifecycle.update(1, { trackErrors: true });
expect(state.updates).toEqual([expect.objectContaining({ trackErrors: true })]);
});
it("always lets a Standard feature be turned off", async () => {
state.site = { ...makeSite(), trackFormInteractions: true };
await siteConfigurationLifecycle.update(1, { trackFormInteractions: false });
expect(getSubscriptionInner).not.toHaveBeenCalled();
expect(state.updates).toEqual([expect.objectContaining({ trackFormInteractions: false })]);
});
it("keeps accepting a Standard feature the site already has, e.g. a full-form save", async () => {
state.site = { ...makeSite(), webVitals: true };
await siteConfigurationLifecycle.update(1, { webVitals: true, name: "Renamed" });
expect(getSubscriptionInner).not.toHaveBeenCalled();
expect(state.updates).toEqual([expect.objectContaining({ webVitals: true, name: "Renamed" })]);
});
});
it("owns private-link persistence and invalidation", async () => {
const privateLinkKey = await siteConfigurationLifecycle.updatePrivateLink(1, "generate_private_link_key");
expect(privateLinkKey).toMatch(/^[a-f0-9]{12}$/);
expect(state.updates).toEqual([{ privateLinkKey }]);
expect(mocks.invalidate).toHaveBeenCalledWith(state.site);
});
it("deletes the import rows and the Site row, wipes replay data, then invalidates the Site", async () => {
await siteConfigurationLifecycle.delete(1);
expect(state.deletes).toBe(2);
expect(mocks.clickhouseCommand).toHaveBeenCalledTimes(2);
expect(mocks.invalidate).toHaveBeenCalledWith(state.site);
});
it("queues the replay deletes instead of waiting behind other mutations", async () => {
await siteConfigurationLifecycle.delete(1);
for (const [call] of mocks.clickhouseCommand.mock.calls) {
expect(call.clickhouse_settings).toEqual({ lightweight_deletes_sync: "0" });
}
});
it("keeps replay data when the Postgres delete fails", async () => {
state.deleteError = new Error("foreign key violation");
await expect(siteConfigurationLifecycle.delete(1)).rejects.toThrow("foreign key violation");
expect(mocks.clickhouseCommand).not.toHaveBeenCalled();
expect(mocks.invalidate).not.toHaveBeenCalled();
});
it("rolls the Site back when replay cleanup fails", async () => {
mocks.clickhouseCommand.mockRejectedValueOnce(new Error("clickhouse unavailable"));
await expect(siteConfigurationLifecycle.delete(1)).rejects.toThrow("clickhouse unavailable");
expect(state.deletes).toBe(0);
expect(mocks.invalidate).not.toHaveBeenCalled();
});
});