Skip to content

[test-improver] Improve tests for difc sink_server_ids#2064

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/difc-sink-server-ids-46cd15957bcf1562
Draft

[test-improver] Improve tests for difc sink_server_ids#2064
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/difc-sink-server-ids-46cd15957bcf1562

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/difc/sink_server_ids_test.go (new)
  • Package: internal/difc
  • Lines Added: 223

Improvements Made

1. Increased Coverage — Previously Zero

sink_server_ids.go exports two public functions (SetSinkServerIDs, IsSinkServerID) with zero test coverage. These functions control which backend server IDs receive DIFC tag snapshot enrichment in RPC JSONL logs — a security-relevant configuration path.

2. New Tests Added

TestSetSinkServerIDs (9 table-driven cases)

  • nil input clears configuration (sets global to nil)
  • ✅ empty []string{} input clears configuration (same early-exit path)
  • ✅ single server ID is stored correctly
  • ✅ multiple IDs are stored sorted alphabetically
  • ✅ duplicate IDs are deduplicated
  • ✅ surrounding whitespace is trimmed from each ID
  • ✅ empty/blank strings are skipped
  • ✅ all-blank input (non-empty slice) results in empty (non-nil) slice — distinct from the nil early-exit path
  • ✅ whitespace-trimmed duplicates are also deduplicated

TestSetSinkServerIDs_OverwritesPreviousConfiguration

  • ✅ Verifies a second call completely replaces the first configuration

TestSetSinkServerIDs_ClearWithEmpty

  • ✅ Verifies that calling with []string{} after setting IDs resets to nil

TestIsSinkServerID (8 table-driven cases)

  • ✅ Exact match returns true
  • ✅ Non-match returns false
  • ✅ Empty configuration returns false
  • ✅ Nil configuration returns false
  • ✅ Matches one of several configured IDs
  • ✅ Does not match when ID not in list
  • ✅ Case-sensitive: "GitHub" does not match "github"
  • ✅ Empty query ID does not match non-empty configured IDs

TestIsSinkServerID_AfterClear

  • ✅ Match before clear, no match after SetSinkServerIDs(nil)

TestSetSinkServerIDs_Concurrency

  • ✅ 10 concurrent writers + 10 concurrent readers verify mutex protection (race-detector safe)

3. Test Isolation

Each subtest registers a t.Cleanup hook that resets the package-level sinkServerIDs global through the same mutex used in production, ensuring tests are fully independent regardless of execution order.

Test Execution

Tests use the standard testify patterns established throughout the internal/difc package:

  • assert for non-fatal checks
  • require for assertions that must pass to continue
  • Table-driven format with named subtests
  • t.Cleanup for state teardown

Why These Changes?

sink_server_ids.go had 0% test coverage despite being a critical security-configuration component: it determines which backend servers receive enriched DIFC tag data in audit logs. The logic has several non-trivial invariants (sort order, deduplication, whitespace trimming, nil-vs-empty-slice distinction in two code paths) that are easy to break silently. These tests lock in all those invariants.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver ·

Cover SetSinkServerIDs: nil/empty input clears state, IDs are
sorted and deduplicated, whitespace is trimmed, empty strings
are skipped, and repeated calls overwrite prior configuration.

Cover IsSinkServerID: matching, non-matching, case-sensitivity,
empty query, and nil/empty configuration all behave correctly.

Add concurrency test to verify mutex protection under parallel
reads and writes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants