Files
wehub-resource-sync e30e75b5d4
Code Quality / Oxlint + Oxfmt (push) Waiting to run
Code Quality / Template Sync (push) Waiting to run
Code Quality / Build Changed Packages (push) Waiting to run
Code Quality / Test Changed Packages (push) Waiting to run
Deploy Expo Example / Deploy Production (push) Waiting to run
Deploy Ink Example / Deploy Production (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.12) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Waiting to run
Deploy Shadcn Registry / Deploy Production (push) Waiting to run
Template Metrics / LOC + Bundle Size (push) Waiting to run
Changesets / Create Version PR (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

110 lines
2.4 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { analytics } from "./analytics";
vi.mock("@vercel/analytics", () => ({
track: vi.fn(),
}));
const globalObject = globalThis as {
window?: {
umami?: unknown;
posthog?: {
capture?: (
event: string,
properties?: Record<string, string | number | boolean>,
) => void;
};
};
};
const previousWindow = globalObject.window;
afterEach(() => {
if (previousWindow === undefined) {
delete globalObject.window;
} else {
globalObject.window = previousWindow;
}
});
it("analytics does not throw when umami exists without track", () => {
globalObject.window = {
umami: {},
};
expect(() => {
analytics.cta.clicked("get_started", "header");
}).not.toThrow();
});
it("tracks assistant feedback lifecycle events to PostHog", () => {
const capture = vi.fn();
globalObject.window = {
posthog: {
capture,
},
};
analytics.assistant.feedbackShown({
threadId: "thread-1",
messageId: "message-1",
user_question_length: 12,
assistant_response_length: 34,
tool_calls_count: 1,
tool_names: "readDoc",
});
analytics.assistant.feedbackClicked({
threadId: "thread-1",
messageId: "message-1",
type: "negative",
category: "wrong_information",
comment_length: 24,
user_question_length: 12,
assistant_response_length: 34,
tool_calls_count: 1,
tool_names: "readDoc",
});
analytics.assistant.feedbackSubmitFailed({
threadId: "thread-1",
messageId: "message-1",
type: "negative",
category: "wrong_information",
comment_length: 24,
user_question_length: 12,
assistant_response_length: 34,
tool_calls_count: 1,
tool_names: "readDoc",
error_name: "TypeError",
error_message: "Failed to submit feedback",
});
expect(capture).toHaveBeenNthCalledWith(
1,
"assistant_feedback_shown",
expect.objectContaining({
threadId: "thread-1",
messageId: "message-1",
}),
);
expect(capture).toHaveBeenNthCalledWith(
2,
"assistant_feedback_clicked",
expect.objectContaining({
type: "negative",
category: "wrong_information",
}),
);
expect(capture).toHaveBeenNthCalledWith(
3,
"assistant_feedback_submit_failed",
expect.objectContaining({
error_name: "TypeError",
error_message: "Failed to submit feedback",
}),
);
});