Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions __tests__/components/user/brain/UserPageBrainActivity.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { render, screen } from "@testing-library/react";
import UserPageBrainActivity from "@/components/user/brain/UserPageBrainActivity";
import { useIdentityActivity } from "@/hooks/useIdentityActivity";
import { Time } from "@/helpers/time";

jest.mock("@/hooks/useIdentityActivity", () => ({
useIdentityActivity: jest.fn(),
}));

const mockedUseIdentityActivity = useIdentityActivity as jest.MockedFunction<
typeof useIdentityActivity
>;
const DAY_MS = Time.days(1).toMillis();

function buildActivityResponse() {
const lastDate = new Date(Date.UTC(2026, 2, 16));
const startMs = lastDate.getTime() - 364 * DAY_MS;

return {
last_date: "16.03.2026",
date_samples: Array.from({ length: 365 }, (_, index) => {
const isoDate = new Date(startMs + index * DAY_MS)
.toISOString()
.slice(0, 10);

switch (isoDate) {
case "2026-01-01":
return 1;
case "2026-02-01":
return 2;
case "2026-03-03":
return 3;
case "2026-03-04":
return 4;
case "2026-03-16":
return 2;
default:
return 0;
}
}),
};
}

describe("UserPageBrainActivity", () => {
beforeEach(() => {
mockedUseIdentityActivity.mockReset();
});

it("renders a loading skeleton", () => {
mockedUseIdentityActivity.mockReturnValue({
status: "pending",
data: undefined,
} as any);

render(
<UserPageBrainActivity
profile={{ handle: "alice", primary_wallet: "0x1" } as any}
/>
);

expect(screen.getByTestId("brain-activity-card")).toBeInTheDocument();
expect(
screen.getByLabelText("Loading activity heatmap")
).toBeInTheDocument();
});

it("renders a compact error state", () => {
mockedUseIdentityActivity.mockReturnValue({
status: "error",
error: new Error("boom"),
data: undefined,
} as any);

render(
<UserPageBrainActivity
profile={{ handle: "alice", primary_wallet: "0x1" } as any}
/>
);

expect(screen.getByText("Unable to load activity.")).toBeInTheDocument();
});

it("renders the activity bars and summary", () => {
mockedUseIdentityActivity.mockReturnValue({
status: "success",
data: buildActivityResponse(),
} as any);

render(
<UserPageBrainActivity
profile={{ handle: "alice", primary_wallet: "0x1" } as any}
/>
);

expect(mockedUseIdentityActivity).toHaveBeenCalledWith({
identity: "alice",
enabled: true,
});
expect(screen.getByText("12 drops in 2026")).toBeInTheDocument();
expect(
screen.getByRole("img", { name: "Jan 1, 2026: 1 drop" })
).toBeInTheDocument();
expect(
screen.getByRole("img", { name: "Mar 16, 2026: 2 drops" })
).toBeInTheDocument();
expect(
screen.getByLabelText("Activity heatmap for 2026")
).toBeInTheDocument();
expect(screen.getByText("Jan")).toBeInTheDocument();
expect(screen.getByText("Feb")).toBeInTheDocument();
expect(screen.getByText("Mar")).toBeInTheDocument();
});

it("falls back to the primary wallet when the handle is missing", () => {
mockedUseIdentityActivity.mockReturnValue({
status: "success",
data: buildActivityResponse(),
} as any);

render(
<UserPageBrainActivity
profile={{ handle: null, primary_wallet: "0xabc" } as any}
/>
);

expect(mockedUseIdentityActivity).toHaveBeenCalledWith({
identity: "0xabc",
enabled: true,
});
});
});
6 changes: 6 additions & 0 deletions __tests__/components/user/brain/UserPageDrops.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ jest.mock("@/components/drops/view/Drops", () => ({
__esModule: true,
default: () => <div data-testid="drops" />,
}));
jest.mock("@/components/user/brain/UserPageBrainActivity", () => ({
__esModule: true,
default: () => <div data-testid="brain-activity-card" />,
}));
jest.mock("@/components/user/brain/UserPageBrainSidebar", () => ({
__esModule: true,
default: () => <div data-testid="brain-sidebar" />,
Expand All @@ -15,12 +19,14 @@ describe("UserPageDrops", () => {
const { getByTestId } = render(
<UserPageDrops profile={{ handle: "test" } as any} />
);
expect(getByTestId("brain-activity-card")).toBeInTheDocument();
expect(getByTestId("drops")).toBeInTheDocument();
expect(getByTestId("brain-sidebar")).toBeInTheDocument();
});

it("hides Drops when no profile handle", () => {
const { queryByTestId } = render(<UserPageDrops profile={null} />);
expect(queryByTestId("brain-activity-card")).toBeNull();
expect(queryByTestId("drops")).toBeNull();
expect(queryByTestId("brain-sidebar")).toBeNull();
});
Expand Down
195 changes: 195 additions & 0 deletions __tests__/components/user/brain/userPageBrainActivity.helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import {
buildUserPageBrainActivityViewModel,
type UserPageBrainActivityResponse,
} from "@/components/user/brain/userPageBrainActivity.helpers";
import { Time } from "@/helpers/time";

const DAY_MS = Time.days(1).toMillis();

function formatBackendDate(date: Date): string {
return [
date.getUTCDate().toString().padStart(2, "0"),
(date.getUTCMonth() + 1).toString().padStart(2, "0"),
date.getUTCFullYear().toString(),
].join(".");
}

function buildResponse({
lastDate,
countsByIsoDate,
}: {
lastDate: Date;
countsByIsoDate?: Record<string, number> | undefined;
}): UserPageBrainActivityResponse {
const startMs = lastDate.getTime() - 364 * DAY_MS;

return {
last_date: formatBackendDate(lastDate),
date_samples: Array.from({ length: 365 }, (_, index) => {
const isoDate = new Date(startMs + index * DAY_MS)
.toISOString()
.slice(0, 10);
return countsByIsoDate?.[isoDate] ?? 0;
}),
};
}

describe("buildUserPageBrainActivityViewModel", () => {
it("builds a heatmap view model from the activity response", () => {
const activity = buildUserPageBrainActivityViewModel(
buildResponse({
lastDate: new Date(Date.UTC(2026, 2, 16)),
countsByIsoDate: {
"2026-01-01": 1,
"2026-01-15": 3,
"2026-02-01": 8,
"2026-03-03": 20,
"2026-03-04": 50,
},
})
);

expect(activity).not.toBeNull();
expect(activity?.periodLabel).toBe("the last 12 months");
expect(activity?.totalDrops).toBe(82);
expect(activity?.weekCount).toBeGreaterThan(0);
expect(activity?.monthLabels).toEqual(
expect.arrayContaining([
expect.objectContaining({ label: "Jan" }),
expect.objectContaining({ label: "Feb" }),
expect.objectContaining({ label: "Mar" }),
])
);
expect(activity?.cells.length).toBe((activity?.weekCount ?? 0) * 7);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-01-01")
).toMatchObject({
count: 1,
state: "active",
intensity: 1,
ariaLabel: "Jan 1, 2026: 1 public post",
});
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-01-15")
).toMatchObject({
count: 3,
intensity: 1,
});
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-02-01")
).toMatchObject({
count: 8,
intensity: 2,
});
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-03")
).toMatchObject({
count: 20,
intensity: 3,
});
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-04")
).toMatchObject({
count: 50,
intensity: 4,
});
});

it("uses per-profile quantile buckets for active-day intensity", () => {
const activity = buildUserPageBrainActivityViewModel(
buildResponse({
lastDate: new Date(Date.UTC(2026, 2, 16)),
countsByIsoDate: {
"2026-03-03": 1,
"2026-03-04": 2,
"2026-03-05": 3,
"2026-03-06": 7,
"2026-03-07": 8,
"2026-03-08": 19,
"2026-03-09": 20,
"2026-03-10": 49,
},
})
);

expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-03")?.intensity
).toBe(1);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-04")?.intensity
).toBe(1);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-05")?.intensity
).toBe(2);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-06")?.intensity
).toBe(2);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-07")?.intensity
).toBe(3);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-08")?.intensity
).toBe(3);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-09")?.intensity
).toBe(4);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-10")?.intensity
).toBe(4);
});

it("scales intensity relative to each profile rather than global thresholds", () => {
const activity = buildUserPageBrainActivityViewModel(
buildResponse({
lastDate: new Date(Date.UTC(2026, 2, 16)),
countsByIsoDate: {
"2026-03-10": 1,
"2026-03-11": 20,
"2026-03-12": 155,
"2026-03-13": 500,
},
})
);

expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-10")?.intensity
).toBe(1);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-11")?.intensity
).toBe(2);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-12")?.intensity
).toBe(3);
expect(
activity?.cells.find((cell) => cell.isoDate === "2026-03-13")?.intensity
).toBe(4);
});

it("returns null for an invalid anchor date", () => {
expect(
buildUserPageBrainActivityViewModel({
last_date: "2026-03-16",
date_samples: [1, 2, 3],
})
).toBeNull();
});

it("keeps empty years renderable", () => {
const activity = buildUserPageBrainActivityViewModel(
buildResponse({
lastDate: new Date(Date.UTC(2026, 1, 28)),
})
);

expect(activity?.totalDrops).toBe(0);
expect(activity?.weekCount).toBeGreaterThan(0);
expect(activity?.cells.length).toBe(
activity?.weekCount ? activity.weekCount * 7 : 0
);
expect(
activity?.cells.every((cell) =>
cell.state === "padding" ? true : cell.state === "empty"
)
).toBe(true);
});
});
1 change: 1 addition & 0 deletions components/react-query-wrapper/ReactQueryWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export enum QueryKey {
IDENTITY_TDH_STATS = "IDENTITY_TDH_STATS",
GLOBAL_TDH_STATS = "GLOBAL_TDH_STATS",
IDENTITY_AVAILABLE_CREDIT = "IDENTITY_AVAILABLE_CREDIT",
IDENTITY_ACTIVITY = "IDENTITY_ACTIVITY",
IDENTITY_FOLLOWING_ACTIONS = "IDENTITY_FOLLOWING_ACTIONS",
IDENTITY_FOLLOWERS = "IDENTITY_FOLLOWERS",
IDENTITY_NOTIFICATIONS = "IDENTITY_NOTIFICATIONS",
Expand Down
Loading
Loading