feat(analytics): add daily trend and activity heatmap endpoints, and implement corresponding frontend components
- Added new API endpoints for daily trend data and activity heatmap in analyticsRoutes.ts. - Created new frontend components: ActivityChart, ControlBar, Heatmap, SummaryCards, TopicList, TrendChart, UserTable, and ViolatorTable for displaying analytics data. - Implemented loading and empty states in the new components. - Enhanced the existing moderation tests with remote fallback handling for Indonesian text normalization.
This commit is contained in:
@@ -1,4 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("../../src/moderation/indonesianTextNormalizer.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../../src/moderation/indonesianTextNormalizer.js")>();
|
||||
return {
|
||||
...actual,
|
||||
formatModerationTextEvidenceForPrompt: vi.fn(async (content: string) => {
|
||||
// Deterministic mock evidence — length tuned for the "tight budget" test:
|
||||
// maxTokens=300, target ~88, c3 ~108, c2 ~108, c1 ~108
|
||||
// Expectation: target+c3 fits (196), target+c3+c2 overflows (304)
|
||||
return "[text_evidence] categories=[\"offensive\",\"profanity\",\"sexual_violence\"] severity=high confidence=0.92 language=id detected=badword normalized=false metadata_v2=true context=true";
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
import {
|
||||
buildConversationContext,
|
||||
estimateTokens,
|
||||
@@ -6,6 +20,10 @@ import {
|
||||
} from "../../src/moderation/conversationContext";
|
||||
import type { MessageRecord } from "../../src/moderation/types";
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function message(
|
||||
id: string,
|
||||
content: string,
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { config } from "../../src/config";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
axiosPost: vi.fn(),
|
||||
openaiCreate: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("axios", () => ({
|
||||
default: {
|
||||
post: mocks.axiosPost,
|
||||
isAxiosError: (error: unknown) =>
|
||||
Boolean(
|
||||
error &&
|
||||
typeof error === "object" &&
|
||||
"isAxiosError" in error &&
|
||||
(error as { isAxiosError?: unknown }).isAxiosError,
|
||||
),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("openai", () => ({
|
||||
default: class MockOpenAI {
|
||||
chat = {
|
||||
completions: {
|
||||
create: mocks.openaiCreate,
|
||||
},
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
describe("detectIndonesianBadwords remote fallback", () => {
|
||||
beforeEach(() => {
|
||||
mocks.axiosPost.mockReset();
|
||||
mocks.openaiCreate.mockReset();
|
||||
config.NVIDIA_NEMOTRON_API_KEY = "test-nemotron-key";
|
||||
config.AI_LLM_API_KEY = "test-primary-key";
|
||||
});
|
||||
|
||||
it("falls back to primary AI after Nemotron rate limits and caches the result", async () => {
|
||||
mocks.axiosPost.mockRejectedValue({
|
||||
isAxiosError: true,
|
||||
response: { status: 429 },
|
||||
message: "Too Many Requests",
|
||||
});
|
||||
mocks.openaiCreate.mockResolvedValue({
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: JSON.stringify({ flags: ["harassment"] }),
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const { detectIndonesianBadwords } = await import(
|
||||
"../../src/moderation/indonesianTextNormalizer"
|
||||
);
|
||||
|
||||
const first = await detectIndonesianBadwords("squad jump soalnya");
|
||||
const second = await detectIndonesianBadwords("squad jump soalnya");
|
||||
|
||||
expect(first).toEqual(["harassment"]);
|
||||
expect(second).toEqual(["harassment"]);
|
||||
expect(mocks.axiosPost).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.openaiCreate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterAll, afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildModerationTextEvidence,
|
||||
detectIndonesianBadwords,
|
||||
@@ -6,6 +6,26 @@ import {
|
||||
normalizeDiscordCustomEmoji,
|
||||
normalizeIndonesianSlang,
|
||||
} from "../../src/moderation/indonesianTextNormalizer";
|
||||
import { config } from "../../src/config";
|
||||
|
||||
const originalNemotronKey = config.NVIDIA_NEMOTRON_API_KEY;
|
||||
const originalPrimaryAiKey = config.AI_LLM_API_KEY;
|
||||
|
||||
function disableRemoteModeration(): void {
|
||||
config.NVIDIA_NEMOTRON_API_KEY = undefined;
|
||||
config.AI_LLM_API_KEY = undefined;
|
||||
}
|
||||
|
||||
disableRemoteModeration();
|
||||
|
||||
afterEach(() => {
|
||||
disableRemoteModeration();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
config.NVIDIA_NEMOTRON_API_KEY = originalNemotronKey;
|
||||
config.AI_LLM_API_KEY = originalPrimaryAiKey;
|
||||
});
|
||||
|
||||
describe("normalizeDiscordCustomEmoji", () => {
|
||||
it("replaces static custom emoji", () => {
|
||||
|
||||
Reference in New Issue
Block a user