import { TrackClient, RegionUS } from "customerio-node";
const TEST_RUN_ID = `ci-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
const createdUsers: string[] = [];
export function getCioTestClient(): TrackClient {
return new TrackClient(
process.env.CUSTOMERIO_SITE_ID!,
process.env.CUSTOMERIO_TRACK_API_KEY!,
{ region: RegionUS }
);
}
export function testUserId(label: string): string {
const id = `${TEST_RUN_ID}-${label}`;
createdUsers.push(id);
return id;
}
export async function cleanupTestUsers(client: TrackClient): Promise<void> {
console.log(`Cleaning up ${createdUsers.length} test users...`);
for (const userId of createdUsers) {
try {
await client.suppress(userId);
await client.destroy(userId);
} catch {
}
}
createdUsers.length = 0;
}
import { describe, it, expect, afterAll } from "vitest";
import { getCioTestClient, testUserId, cleanupTestUsers } from "./helpers/cio-test-utils";
const cio = getCioTestClient();
describe("Customer.io Integration", () => {
afterAll(async () => {
await cleanupTestUsers(cio);
});
it("should identify a new user", async () => {
const userId = testUserId("identify-new");
await expect(
cio.identify(userId, {
email: `${userId}@test.example.com`,
created_at: Math.floor(Date.now() / 1000),
})
).resolves.not.toThrow();
});
it("should update an existing user", async () => {
const userId = testUserId("identify-update");
await cio.identify(userId, { email: `${userId}@test.example.com` });
await expect(
cio.identify(userId, { plan: "pro", updated: true })
).resolves.not.toThrow();
});
it("should track an event on a user", async () => {
const userId = testUserId("track-event");
await cio.identify(userId, { email: `${userId}@test.example.com` });
await expect(
cio.track(userId, {
name: "ci_test_event",
data: { test_run: true, timestamp: Date.now() },
})
).resolves.not.toThrow();
});
it("should track an anonymous event", async () => {
await expect(
cio.trackAnonymous({
anonymous_id: testUserId("anon"),
name: "ci_anonymous_test",
data: { page: "/test" },
})
).resolves.not.toThrow();
});
it("should suppress a user", async () => {
const userId = testUserId("suppress");
await cio.identify(userId, { email: `${userId}@test.example.com` });
await expect(cio.suppress(userId)).resolves.not.toThrow();
});
it("should reject invalid credentials", async () => {
const badClient = new (await import("customerio-node")).TrackClient(
"invalid", "invalid", { region: (await import("customerio-node")).RegionUS }
);
await expect(
badClient.identify("x", { email: "x@test.com" })
).rejects.toThrow();
});
});
import { TrackClient, RegionUS } from "customerio-node";
const cio = new TrackClient(
process.env.CUSTOMERIO_SITE_ID!,
process.env.CUSTOMERIO_TRACK_API_KEY!,
{ region: RegionUS }
);
async function cleanup() {
console.log("Cleaning up CI test users...");
console.log("Note: Customer.io doesn't have a list/search API via Track API.");
console.log("Cleanup relies on suppress+destroy for known test user IDs.");
console.log("For bulk cleanup, use the Customer.io dashboard People filter.");
}
cleanup();
{
"lint-staged": {
"lib/customerio-*.ts": ["eslint --fix", "vitest related --run"],
"services/customerio-*.ts": ["eslint --fix", "vitest related --run"]
}
}