Skip to content

Add unit tests for cleanupStaleTrackedRequests#194

Merged
ormidales merged 2 commits into1.0.11from
copilot/add-unit-tests-for-cleanupstaletrackedrequests
Mar 26, 2026
Merged

Add unit tests for cleanupStaleTrackedRequests#194
ormidales merged 2 commits into1.0.11from
copilot/add-unit-tests-for-cleanupstaletrackedrequests

Conversation

Copy link
Contributor

Copilot AI commented Mar 26, 2026

cleanupStaleTrackedRequests was exported and designed with a testable now parameter, but had zero test coverage for TTL-based eviction or entry preservation.

Changes

  • background.js — Extends module.exports to expose cleanupStaleTrackedRequests, initialHostByRequest, redirectedRequestIds, and REQUEST_TRACK_TTL_MS for test access.

  • tests/cleanup.test.js — New test suite with four deterministic scenarios driven by a fixed now value:

    • Stale entries (beyond TTL) evicted from initialHostByRequest
    • Fresh entries (within TTL) preserved
    • Eviction removes the requestId from redirectedRequestIds in sync
    • No-op and no throw on empty map
test("removes entries older than TTL", () => {
  const now = 1_000_000;
  initialHostByRequest.set("req-1", { host: "example.com", trackedAt: now - REQUEST_TRACK_TTL_MS - 1 });
  cleanupStaleTrackedRequests(now);
  expect(initialHostByRequest.has("req-1")).toBe(false);
});
Original prompt

This section details on the original issue you should resolve

<issue_title>[PATCH] cleanupStaleTrackedRequests is exported but has no unit tests</issue_title>
<issue_description>Category: testing
Severity: patch
File(s): background.js, tests/

Description

The function cleanupStaleTrackedRequests is a pure, deterministic utility that accepts an optional now timestamp parameter specifically to facilitate testing. Despite this design, no unit tests cover its behaviour: neither the TTL-based eviction of stale entries from initialHostByRequest and redirectedRequestIds, nor the preservation of entries that are still within the TTL window.

Problematic code example

// background.js
const cleanupStaleTrackedRequests = (now = Date.now()) => {
  for (const [requestId, trackedRequest] of initialHostByRequest.entries()) {
    if (trackedRequest && typeof trackedRequest === "object" && now - trackedRequest.trackedAt > REQUEST_TRACK_TTL_MS) {
      initialHostByRequest.delete(requestId);
      redirectedRequestIds.delete(requestId);
    }
  }
};
// No test file exercises this function

Suggested fix

Add a dedicated test file (e.g. tests/cleanup.test.js) that covers the following scenarios:

const { cleanupStaleTrackedRequests } = require("../background.js");

describe("cleanupStaleTrackedRequests", () => {
  test("removes entries older than TTL", () => { /* ... */ });
  test("preserves entries within TTL", () => { /* ... */ });
  test("also removes requestId from redirectedRequestIds", () => { /* ... */ });
  test("no-op on empty map", () => { /* ... */ });
});

Acceptance criteria

  • A test file exists for cleanupStaleTrackedRequests
  • Stale entries (beyond TTL) are evicted from both initialHostByRequest and redirectedRequestIds
  • Fresh entries (within TTL) are preserved
  • The empty-map case is covered</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: ormidales <46538211+ormidales@users.noreply.github.com>
Agent-Logs-Url: https://github.com/ormidales/truepath/sessions/a1feccd3-722e-411b-a4f4-e20e1583f419
Copilot AI changed the title [WIP] Add unit tests for cleanupStaleTrackedRequests Add unit tests for cleanupStaleTrackedRequests Mar 26, 2026
Copilot AI requested a review from ormidales March 26, 2026 09:42
@ormidales ormidales added this to the v1.0.11 milestone Mar 26, 2026
@ormidales ormidales linked an issue Mar 26, 2026 that may be closed by this pull request
4 tasks
@ormidales ormidales marked this pull request as ready for review March 26, 2026 09:44
Copilot AI review requested due to automatic review settings March 26, 2026 09:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds deterministic Jest unit tests for cleanupStaleTrackedRequests to validate TTL-based eviction behavior and ensure tracking state stays consistent across internal data structures.

Changes:

  • Export cleanupStaleTrackedRequests and its related in-memory tracking structures/constants from background.js for test access.
  • Add tests/cleanup.test.js covering stale eviction, fresh preservation, synced cleanup of redirectedRequestIds, and empty-map no-op behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
background.js Extends module.exports to expose the cleanup helper plus its associated Maps/Sets and TTL constant to Jest.
tests/cleanup.test.js New deterministic unit tests for TTL eviction and state synchronization using a fixed now timestamp.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ormidales ormidales merged commit d2b1520 into 1.0.11 Mar 26, 2026
4 checks passed
@ormidales ormidales deleted the copilot/add-unit-tests-for-cleanupstaletrackedrequests branch March 26, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PATCH] cleanupStaleTrackedRequests is exported but has no unit tests

3 participants