Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(activity): implement duration guard for stopping activities and discard short ones

Khuram Niaz5da2a02d9972
diff --git a/__tests__/ActivitiesContainer.spec.tsx b/__tests__/ActivitiesContainer.spec.tsx
index 3802427..d16c210 100644
--- a/__tests__/ActivitiesContainer.spec.tsx
+++ b/__tests__/ActivitiesContainer.spec.tsx
@@ -585,7 +585,7 @@ describe("ActivitiesContainer Search Functionality", () => {
activity: {
id: "current-1",
activityName: "Ongoing Task",
- startTime: new Date(),
+ startTime: new Date(Date.now() - 5 * 60 * 1000),
endTime: null,
description: null,
activityType: { id: "1", label: "Learning", value: "learning" },
diff --git a/__tests__/ActivityContext.spec.tsx b/__tests__/ActivityContext.spec.tsx
new file mode 100644
index 0000000..236f542
--- /dev/null
+++ b/__tests__/ActivityContext.spec.tsx
@@ -0,0 +1,137 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import {
+ ActivityProvider,
+ useActivity,
+} from "@/context/ActivityContext";
+import {
+ deleteActivityById,
+ getCurrentActivity,
+ startActivityById,
+ stopActivityById,
+} from "@/actions/activity.actions";
+import { toast } from "@/components/ui/use-toast";
+import { APP_CONSTANTS } from "@/lib/constants";
+
+vi.mock("@/actions/activity.actions", () => ({
+ getCurrentActivity: vi.fn(),
+ startActivityById: vi.fn(),
+ stopActivityById: vi.fn(),
+ deleteActivityById: vi.fn(),
+}));
+
+vi.mock("@/components/ui/use-toast", () => ({
+ toast: vi.fn(),
+}));
+
+function TestHarness() {
+ const { currentActivity, startActivity, stopActivity } = useActivity();
+ return (
+ <div>
+ <div data-testid="current">{currentActivity ? currentActivity.id : "none"}</div>
+ <button onClick={() => startActivity("activity-type-1")}>Start</button>
+ <button onClick={() => stopActivity()}>Stop</button>
+ </div>
+ );
+}
+
+describe("ActivityContext stopActivity duration guard", () => {
+ const user = userEvent.setup();
+
+ beforeEach(() => {
+ (getCurrentActivity as any).mockResolvedValue({ success: false });
+ });
+
+ const startWith = async (startTime: Date, id: string) => {
+ (startActivityById as any).mockResolvedValue({
+ success: true,
+ newActivity: {
+ id,
+ activityName: "Test Activity",
+ startTime,
+ activityType: { id: "t1", label: "Coding" },
+ },
+ });
+
+ render(
+ <ActivityProvider>
+ <TestHarness />
+ </ActivityProvider>
+ );
+
+ await user.click(screen.getByText("Start"));
+ await waitFor(() =>
+ expect(screen.getByTestId("current")).toHaveTextContent(id)
+ );
+ };
+
+ it("discards the activity instead of saving it when stopped under the minimum duration", async () => {
+ (deleteActivityById as any).mockResolvedValue({ success: true });
+
+ await startWith(new Date(), "act-short");
+ await user.click(screen.getByText("Stop"));
+
+ await waitFor(() => {
+ expect(deleteActivityById).toHaveBeenCalledWith("act-short");
+ });
+ expect(stopActivityById).not.toHaveBeenCalled();
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ description: expect.stringContaining(
+ `less than ${APP_CONSTANTS.ACTIVITY_MIN_DURATION_MINUTES} minutes`
+ ),
+ })
+ );
+ await waitFor(() =>
+ expect(screen.getByTestId("current")).toHaveTextContent("none")
+ );
+ });
+
+ it("saves the activity normally when duration meets the minimum", async () => {
+ (stopActivityById as any).mockResolvedValue({ success: true });
+
+ const startTime = new Date(
+ Date.now() - (APP_CONSTANTS.ACTIVITY_MIN_DURATION_MINUTES + 3) * 60 * 1000
+ );
+ await startWith(startTime, "act-long");
+ await user.click(screen.getByText("Stop"));
+
+ await waitFor(() => {
+ expect(stopActivityById).toHaveBeenCalledWith(
+ "act-long",
+ expect.any(Date),
+ expect.any(Number)
+ );
+ });
+ expect(deleteActivityById).not.toHaveBeenCalled();
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({ variant: "success" })
+ );
+ await waitFor(() =>
+ expect(screen.getByTestId("current")).toHaveTextContent("none")
+ );
+ });
+
+ it("shows an error toast and keeps the activity when discarding fails", async () => {
+ (deleteActivityById as any).mockResolvedValue({
+ success: false,
+ message: "Failed to delete activity.",
+ });
+
+ await startWith(new Date(), "act-fail");
+ await user.click(screen.getByText("Stop"));
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Error!",
+ description: "Failed to delete activity.",
+ })
+ );
+ });
+ expect(stopActivityById).not.toHaveBeenCalled();
+ expect(screen.getByTestId("current")).toHaveTextContent("act-fail");
+ });
+});
diff --git a/src/context/ActivityContext.tsx b/src/context/ActivityContext.tsx
index a57ddeb..cd7fae7 100644
--- a/src/context/ActivityContext.tsx
+++ b/src/context/ActivityContext.tsx
@@ -10,6 +10,7 @@ import {
} from "react";
import { differenceInMilliseconds, differenceInMinutes } from "date-fns";
import {
+ deleteActivityById,
getCurrentActivity,
startActivityById,
stopActivityById,
@@ -95,6 +96,31 @@ export function ActivityProvider({ children }: { children: ReactNode }) {
maxDurationMinutes
);
+ if (duration < APP_CONSTANTS.ACTIVITY_MIN_DURATION_MINUTES) {
+ const { success, message } = await deleteActivityById(
+ currentActivity.id!
+ );
+
+ if (!isMountedRef.current) return success;
+
+ if (success) {
+ stopTimer();
+ setCurrentActivity(undefined);
+ toast({
+ variant: "destructive",
+ description: `Activity not saved because duration was less than ${APP_CONSTANTS.ACTIVITY_MIN_DURATION_MINUTES} minutes`,
+ });
+ return true;
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ return false;
+ }
+ }
+
const { success, message } = await stopActivityById(
currentActivity.id!,
now,
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index 237d23b..9890d47 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -23,6 +23,7 @@ export const APP_CONSTANTS = {
MIN_RESUME_SECTIONS_FOR_SELECTION: 2,
ACTIVITY_MAX_DURATION_MINUTES: 8 * 60, // 8 Hours
ACTIVITY_MAX_DURATION_MS: 8 * 60 * 60 * 1000, // 8 hours in milliseconds
+ ACTIVITY_MIN_DURATION_MINUTES: 2, // activities shorter than this are discarded
RECENT_NUM_JOBS_ACTIVITIES: 7,
AI_SLOW_RESPONSE_THRESHOLD_MS: 15_000, // 15 seconds
INTERSECTION_OBSERVER_THRESHOLD: 0.1,