mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
* chore(deps-dev): bump the dev-deps group across 1 directory with 7 updates Bumps the dev-deps group with 7 updates in the / directory: | Package | From | To | | --- | --- | --- | | [@biomejs/biome](https://github.com/biomejs/biome/tree/HEAD/packages/@biomejs/biome) | `2.4.15` | `2.4.16` | | [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8) | `3.2.4` | `3.2.6` | | [turbo](https://github.com/vercel/turborepo) | `2.9.14` | `2.9.16` | | [vitest](https://github.com/vitest-dev/vitest/tree/HEAD/packages/vitest) | `3.2.4` | `3.2.6` | | [@types/opentype.js](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/opentype.js) | `1.3.9` | `1.3.10` | | [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) | `6.4.2` | `6.4.3` | | [vitepress-plugin-llms](https://github.com/okineadev/vitepress-plugin-llms) | `1.12.2` | `1.13.1` | Updates `@biomejs/biome` from 2.4.15 to 2.4.16 - [Release notes](https://github.com/biomejs/biome/releases) - [Changelog](https://github.com/biomejs/biome/blob/main/packages/@biomejs/biome/CHANGELOG.md) - [Commits](https://github.com/biomejs/biome/commits/@biomejs/biome@2.4.16/packages/@biomejs/biome) Updates `@vitest/coverage-v8` from 3.2.4 to 3.2.6 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Changelog](https://github.com/vitest-dev/vitest/blob/main/docs/releases.md) - [Commits](https://github.com/vitest-dev/vitest/commits/v3.2.6/packages/coverage-v8) Updates `turbo` from 2.9.14 to 2.9.16 - [Release notes](https://github.com/vercel/turborepo/releases) - [Changelog](https://github.com/vercel/turborepo/blob/main/RELEASE.md) - [Commits](https://github.com/vercel/turborepo/compare/v2.9.14...v2.9.16) Updates `vitest` from 3.2.4 to 3.2.6 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Changelog](https://github.com/vitest-dev/vitest/blob/main/docs/releases.md) - [Commits](https://github.com/vitest-dev/vitest/commits/v3.2.6/packages/vitest) Updates `@types/opentype.js` from 1.3.9 to 1.3.10 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/opentype.js) Updates `vite` from 6.4.2 to 6.4.3 - [Release notes](https://github.com/vitejs/vite/releases) - [Changelog](https://github.com/vitejs/vite/blob/v6.4.3/packages/vite/CHANGELOG.md) - [Commits](https://github.com/vitejs/vite/commits/v6.4.3/packages/vite) Updates `vitepress-plugin-llms` from 1.12.2 to 1.13.1 - [Release notes](https://github.com/okineadev/vitepress-plugin-llms/releases) - [Commits](https://github.com/okineadev/vitepress-plugin-llms/compare/v1.12.2...v1.13.1) --- updated-dependencies: - dependency-name: "@biomejs/biome" dependency-version: 2.4.16 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: "@vitest/coverage-v8" dependency-version: 3.2.6 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: turbo dependency-version: 2.9.16 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: vitest dependency-version: 3.2.6 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: "@types/opentype.js" dependency-version: 1.3.10 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: vite dependency-version: 6.4.3 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-deps - dependency-name: vitepress-plugin-llms dependency-version: 1.13.1 dependency-type: direct:development update-type: version-update:semver-minor dependency-group: dev-deps ... Signed-off-by: dependabot[bot] <support@github.com> * fix(test): resolve vitest 3.2.6 mock path breakage for posthog-node and @sentry/node Add posthog-node and @sentry/node to vitest resolve aliases (matching the existing pattern for all other api-workspace packages) and switch the analytics test mocks from fragile relative node_modules paths to bare specifiers. Vitest 3.2.6 changed how it matches mock paths through pnpm symlinks after vi.resetModules(), causing 9 analytics test failures. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
310 lines
10 KiB
TypeScript
310 lines
10 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const config = vi.hoisted(() => ({
|
|
ANALYTICS_ENABLED: false,
|
|
POSTHOG_API_KEY: "",
|
|
POSTHOG_HOST: "",
|
|
SENTRY_DSN: "",
|
|
ANALYTICS_SAMPLE_RATE: 1.0,
|
|
}));
|
|
|
|
const dbGetResult = vi.hoisted(() => ({ value: null as unknown }));
|
|
const mockAuthUser = vi.hoisted(() => ({
|
|
value: null as { id: string; analyticsEnabled?: boolean } | null,
|
|
}));
|
|
|
|
const mockCapture = vi.hoisted(() => vi.fn());
|
|
const mockShutdown = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
|
const MockPostHog = vi.hoisted(() =>
|
|
vi.fn().mockImplementation(() => ({
|
|
capture: mockCapture,
|
|
shutdown: mockShutdown,
|
|
})),
|
|
);
|
|
|
|
const mockSentryCapture = vi.hoisted(() => vi.fn());
|
|
const mockSentryClose = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
|
const mockSentryInit = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("../../../apps/api/src/config.js", () => ({ env: config }));
|
|
|
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
|
db: {
|
|
select: () => ({
|
|
from: () => ({
|
|
where: () => ({
|
|
get: () => dbGetResult.value,
|
|
}),
|
|
}),
|
|
}),
|
|
},
|
|
schema: {
|
|
settings: { key: "key" },
|
|
users: { id: "id", analyticsEnabled: "analyticsEnabled" },
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../../apps/api/src/plugins/auth.js", () => ({
|
|
getAuthUser: () => mockAuthUser.value,
|
|
}));
|
|
|
|
vi.mock("drizzle-orm", () => ({
|
|
eq: () => "mocked-eq",
|
|
}));
|
|
|
|
vi.mock("posthog-node", () => ({
|
|
PostHog: MockPostHog,
|
|
}));
|
|
|
|
vi.mock("@sentry/node", () => ({
|
|
init: mockSentryInit,
|
|
captureException: mockSentryCapture,
|
|
close: mockSentryClose,
|
|
}));
|
|
|
|
type AnalyticsModule = typeof import("../../../apps/api/src/lib/analytics.js");
|
|
let mod: AnalyticsModule;
|
|
|
|
beforeEach(async () => {
|
|
config.ANALYTICS_ENABLED = false;
|
|
config.POSTHOG_API_KEY = "";
|
|
config.POSTHOG_HOST = "";
|
|
config.SENTRY_DSN = "";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
|
|
dbGetResult.value = null;
|
|
mockAuthUser.value = null;
|
|
|
|
mockCapture.mockClear();
|
|
mockShutdown.mockClear();
|
|
MockPostHog.mockClear();
|
|
mockSentryCapture.mockClear();
|
|
mockSentryClose.mockClear();
|
|
mockSentryInit.mockClear();
|
|
|
|
vi.resetModules();
|
|
mod = await import("../../../apps/api/src/lib/analytics.js");
|
|
});
|
|
|
|
describe("initAnalytics", () => {
|
|
it("does nothing when ANALYTICS_ENABLED is false", async () => {
|
|
config.ANALYTICS_ENABLED = false;
|
|
await expect(mod.initAnalytics()).resolves.toBeUndefined();
|
|
expect(MockPostHog).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does nothing when ANALYTICS_ENABLED is true but POSTHOG_API_KEY is empty", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "";
|
|
await expect(mod.initAnalytics()).resolves.toBeUndefined();
|
|
expect(MockPostHog).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("initializes posthog when enabled with API key", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.POSTHOG_HOST = "https://test.posthog.com";
|
|
await mod.initAnalytics();
|
|
|
|
expect(MockPostHog).toHaveBeenCalledWith("phc_test_key", {
|
|
host: "https://test.posthog.com",
|
|
flushAt: 20,
|
|
flushInterval: 30000,
|
|
});
|
|
});
|
|
|
|
it("initializes sentry when SENTRY_DSN is provided", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.SENTRY_DSN = "https://test@sentry.io/123";
|
|
await mod.initAnalytics();
|
|
expect(mockSentryInit).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
dsn: "https://test@sentry.io/123",
|
|
sendDefaultPii: false,
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("captureException", () => {
|
|
it("does nothing when sentryModule is null", () => {
|
|
expect(() => mod.captureException(new Error("test"))).not.toThrow();
|
|
});
|
|
|
|
it("does nothing when request user is not opted in", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.SENTRY_DSN = "https://test@sentry.io/123";
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = null;
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.captureException>[1];
|
|
mod.captureException(new Error("test"), fakeRequest);
|
|
expect(mockSentryCapture).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("captures when sentry is initialized and no request provided", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.SENTRY_DSN = "https://test@sentry.io/123";
|
|
await mod.initAnalytics();
|
|
|
|
const err = new Error("test error");
|
|
mod.captureException(err);
|
|
expect(mockSentryCapture).toHaveBeenCalledWith(err);
|
|
});
|
|
|
|
it("captures when sentry is initialized and request user is opted in", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.SENTRY_DSN = "https://test@sentry.io/123";
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = { id: "user-1", analyticsEnabled: true };
|
|
dbGetResult.value = { analyticsEnabled: true };
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.captureException>[1];
|
|
const err = new Error("opted in error");
|
|
mod.captureException(err, fakeRequest);
|
|
expect(mockSentryCapture).toHaveBeenCalledWith(err);
|
|
});
|
|
});
|
|
|
|
describe("shutdownAnalytics", () => {
|
|
it("resolves without error when no clients initialized", async () => {
|
|
await expect(mod.shutdownAnalytics()).resolves.toBeUndefined();
|
|
});
|
|
|
|
it("shuts down posthog when initialized", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
await mod.initAnalytics();
|
|
await mod.shutdownAnalytics();
|
|
expect(mockShutdown).toHaveBeenCalled();
|
|
});
|
|
|
|
it("closes sentry when initialized", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.SENTRY_DSN = "https://test@sentry.io/123";
|
|
await mod.initAnalytics();
|
|
await mod.shutdownAnalytics();
|
|
expect(mockSentryClose).toHaveBeenCalledWith(2000);
|
|
});
|
|
});
|
|
|
|
describe("trackEvent", () => {
|
|
it("does nothing when posthogClient is null", () => {
|
|
const fakeRequest = {} as Parameters<typeof mod.trackEvent>[0];
|
|
expect(() => mod.trackEvent(fakeRequest, "test_event", { key: "value" })).not.toThrow();
|
|
});
|
|
|
|
it("does nothing when ANALYTICS_ENABLED is false", () => {
|
|
config.ANALYTICS_ENABLED = false;
|
|
const fakeRequest = {} as Parameters<typeof mod.trackEvent>[0];
|
|
expect(() => mod.trackEvent(fakeRequest, "test_event", { key: "value" })).not.toThrow();
|
|
});
|
|
|
|
it("does nothing when request user is not opted in", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = null;
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "test_event", { key: "value" });
|
|
expect(mockCapture).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does nothing when ANALYTICS_SAMPLE_RATE is 0", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 0;
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = { id: "user-1", analyticsEnabled: true };
|
|
dbGetResult.value = { analyticsEnabled: true };
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "test_event", { key: "value" });
|
|
expect(mockCapture).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("captures event when all conditions are met", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = { id: "user-1", analyticsEnabled: true };
|
|
dbGetResult.value = { analyticsEnabled: true };
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "tool_used", { tool: "resize" });
|
|
expect(mockCapture).toHaveBeenCalledWith({
|
|
distinctId: "unknown",
|
|
event: "tool_used",
|
|
properties: { tool: "resize" },
|
|
});
|
|
});
|
|
|
|
it("uses instance ID from DB when available", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
await mod.initAnalytics();
|
|
|
|
dbGetResult.value = { value: "inst-abc-123", analyticsEnabled: true };
|
|
mockAuthUser.value = { id: "user-1", analyticsEnabled: true };
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "tool_used", { tool: "crop" });
|
|
expect(mockCapture).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
distinctId: "inst-abc-123",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("allows anonymous users with x-analytics-consent header", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = { id: "anonymous" };
|
|
const fakeRequest = {
|
|
headers: { "x-analytics-consent": "true" },
|
|
} as unknown as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "test_event", { key: "value" });
|
|
expect(mockCapture).toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects anonymous users without consent header", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
await mod.initAnalytics();
|
|
|
|
mockAuthUser.value = { id: "anonymous" };
|
|
const fakeRequest = {
|
|
headers: {},
|
|
} as unknown as Parameters<typeof mod.trackEvent>[0];
|
|
mod.trackEvent(fakeRequest, "test_event", { key: "value" });
|
|
expect(mockCapture).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not throw when capture throws internally", async () => {
|
|
config.ANALYTICS_ENABLED = true;
|
|
config.POSTHOG_API_KEY = "phc_test_key";
|
|
config.ANALYTICS_SAMPLE_RATE = 1.0;
|
|
await mod.initAnalytics();
|
|
|
|
mockCapture.mockImplementationOnce(() => {
|
|
throw new Error("capture failed");
|
|
});
|
|
|
|
mockAuthUser.value = { id: "user-1", analyticsEnabled: true };
|
|
dbGetResult.value = { analyticsEnabled: true };
|
|
const fakeRequest = { headers: {} } as Parameters<typeof mod.trackEvent>[0];
|
|
expect(() => mod.trackEvent(fakeRequest, "test_event", { key: "value" })).not.toThrow();
|
|
});
|
|
});
|