Skip to content

fix(creditHelpers): use indexOf instead of findIndex in sortCrewPriority#2740

Open
dougrathbone wants to merge 3 commits intoseerr-team:developfrom
dougrathbone:dougrathbone/fix/crew-sort-priority
Open

fix(creditHelpers): use indexOf instead of findIndex in sortCrewPriority#2740
dougrathbone wants to merge 3 commits intoseerr-team:developfrom
dougrathbone:dougrathbone/fix/crew-sort-priority

Conversation

@dougrathbone
Copy link
Copy Markdown
Contributor

@dougrathbone dougrathbone commented Mar 22, 2026

Description

The sort comparator in sortCrewPriority used findIndex with a substring predicate (job.includes(a.job)) to locate each job's priority rank. Since the preceding .filter() already guarantees every crew member's job is an exact member of priorityJobs, indexOf is correct and simpler.

The substring approach is fragile: if a shorter title were ever added earlier in the list (e.g. 'Pro' before 'Producer'), the substring match would silently return the wrong index and mis-rank those crew members.

Also adds @app/* to the server tsconfig paths so server-side tests can import src/ utilities without relative paths.

AI Disclosure: I used Claude Code to help write the tests. I reviewed and understood all generated code before submitting.

How Has This Been Tested?

New node:test suite at server/utils/creditHelpers.test.ts covering:

  • Filters out jobs not in the priority list
  • Returns an empty array when no crew match priority jobs
  • Returns an empty array for empty input
  • Sorts crew by priority order
  • Handles multiple crew members sharing the same job (asserts both entries are retained and correctly ranked)
  • Correctly ranks Producer, Co-Producer, and Executive Producer independently

All 6 tests pass.

Screenshots / Logs (if applicable)

▶ sortCrewPriority
  ✔ filters out jobs not in the priority list (0.594083ms)
  ✔ returns an empty array when no crew match priority jobs (0.049083ms)
  ✔ returns an empty array for empty input (0.09075ms)
  ✔ sorts crew by priority order (0.052667ms)
  ✔ handles multiple crew members sharing the same job (0.073708ms)
  ✔ correctly ranks Producer, Co-Producer, and Executive Producer independently (0.043792ms)
✔ sortCrewPriority (1.3715ms)
ℹ tests 6
ℹ pass 6
ℹ fail 0

Checklist:

  • I have read and followed the contribution guidelines.
  • Disclosed any use of AI (see our policy)
  • I have updated the documentation accordingly. (no documentation changes required for this fix)
  • All new and existing tests passed.
  • Successful build pnpm build
  • Translation keys pnpm i18n:extract (no new UI strings added)
  • Database migration (if required) (no schema changes)

@dougrathbone dougrathbone requested a review from a team as a code owner March 22, 2026 00:55
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 22, 2026

📝 Walkthrough

Walkthrough

Changed crew-sorting to use exact job equality for priority ranking, added unit tests covering filtering and ordering behavior, and introduced a new TypeScript path alias (@app/*) in the server tsconfig.

Changes

Cohort / File(s) Summary
Credit sorting logic
src/utils/creditHelpers.ts
Replaced substring-based priority lookup (findIndex(...includes(...))) with exact-match lookup via priorityJobs.indexOf(job) when scoring/sorting crew entries.
Server TypeScript config
server/tsconfig.json
Added path alias @app/*../src/* alongside existing aliases in compilerOptions.paths.
Unit tests for sorting
server/utils/creditHelpers.test.ts
New Node node:test suite validating sortCrewPriority behavior: filtering, empty inputs, priority ordering, ties, and producer-rank handling.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰
I nudge the jobs to line up straight,
No fuzzy bits to complicate.
Tests drum soft, the config fits—
A neat little hop, and all commits. 🥕✨

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: replacing findIndex with indexOf in sortCrewPriority, which is the core fix addressed by this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
server/utils/creditHelpers.test.ts (1)

56-65: Tighten same-job assertions to ensure no entries are dropped.

Current checks validate job ordering, but not explicit retention of both producer records.

Suggested test refinement
   const result = sortCrewPriority(crew);
+  assert.equal(result.length, 3);
   assert.equal(result[0].job, 'Director');
   assert.ok(result.slice(1).every((c) => c.job === 'Producer'));
+  assert.deepEqual(
+    result.slice(1).map((c) => c.name).sort(),
+    ['Producer A', 'Producer B']
+  );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/utils/creditHelpers.test.ts` around lines 56 - 65, The test for
sortCrewPriority doesn't assert that both Producer entries are retained; update
the test (using the existing makeCrew and result variables) to assert the result
includes two Producer records and that their names (e.g., 'Producer A' and
'Producer B') are present and not dropped—for example, check result.filter(r =>
r.job === 'Producer').length === 2 and that the set of producer names equals the
expected names while still asserting the Director is first.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@server/utils/creditHelpers.test.ts`:
- Around line 56-65: The test for sortCrewPriority doesn't assert that both
Producer entries are retained; update the test (using the existing makeCrew and
result variables) to assert the result includes two Producer records and that
their names (e.g., 'Producer A' and 'Producer B') are present and not
dropped—for example, check result.filter(r => r.job === 'Producer').length === 2
and that the set of producer names equals the expected names while still
asserting the Director is first.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 76e8424e-9272-40e0-83cf-b1107f3dd3a8

📥 Commits

Reviewing files that changed from the base of the PR and between cdd1c5e and 001c6bb.

📒 Files selected for processing (2)
  • server/tsconfig.json
  • server/utils/creditHelpers.test.ts
✅ Files skipped from review due to trivial changes (1)
  • server/tsconfig.json

The sort comparator used findIndex with a substring predicate
(job.includes(a.job)) to locate each job's rank. Since the preceding
filter already guarantees every crew member's job is an exact member of
priorityJobs, indexOf is correct and simpler.

The substring approach is fragile: if a shorter title were ever added
earlier in the list (e.g. "Pro" before "Producer"), the substring match
would silently return the wrong index and mis-rank those crew members.
Adds @app/* to the server tsconfig paths so tests living under server/
can import src/ utilities without relative paths.

Tests cover: priority-list filtering, empty input, sort ordering across
all priority jobs, multiple crew members sharing the same job, and
independent ranking of Producer, Co-Producer, and Executive Producer.
@dougrathbone dougrathbone force-pushed the dougrathbone/fix/crew-sort-priority branch from 001c6bb to 08d5665 Compare March 22, 2026 10:38
@dougrathbone
Copy link
Copy Markdown
Contributor Author

Good suggestion -- the test now also asserts result.length === 3 (no entries dropped) and that both producer names are present via deepEqual on sorted names. Pushed in the latest commit (also rebased onto develop to pick up the current eslint config).

@dougrathbone
Copy link
Copy Markdown
Contributor Author

@gauthier-th could i get help reviewing this?

@gauthier-th
Copy link
Copy Markdown
Member

@gauthier-th could i get help reviewing this?

Can you provide an example where this fix is improving the sorting order of the crew of a media?

Copy link
Copy Markdown
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

This PR simplifies sortCrewPriority by replacing a substring-based findIndex ranking with an exact-match indexOf, and updates server TypeScript path aliases so server-side tests can import src/ utilities via @app/*.

Changes:

  • Replace findIndex(job.includes(...)) with indexOf(...) in sortCrewPriority to avoid fragile substring matches.
  • Add a new node:test suite covering filtering and sorting behavior for crew job prioritization.
  • Extend server/tsconfig.json path mappings to include @app/*src/* for server-side test imports.

Reviewed changes

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

File Description
src/utils/creditHelpers.ts Uses indexOf for exact priority ranking, avoiding incorrect substring-based matches.
server/utils/creditHelpers.test.ts Adds coverage for filtering and priority sorting behavior, including producer role distinctions.
server/tsconfig.json Adds @app/* path mapping so server tests can import shared app utilities cleanly.

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

@dougrathbone
Copy link
Copy Markdown
Contributor Author

@gauthier-th If you swap the order of Producer and Executive Producer in the priorityJobs array, a crew member with job "Producer" gets sorted at Executive Producer's rank, because "Executive Producer".includes("Producer") is true, and findIndex returns the first match.

The old substring-based comparator only produces correct results by coincidence of the current list ordering.

The test case "correctly ranks Producer, Co-Producer, and Executive Producer independently" demonstrates this.

indexOf is the right fix - the .filter() above already guarantees exact membership, so the sort lookup should be exact too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants