Skip to content

test: cover inventory and translation behavior - #184

Open
nirmalhk7 wants to merge 2 commits into
NTARI-RAND:mainfrom
nirmalhk7:nirmal/agrinet-test-coverage
Open

nirmalhk7 wants to merge 2 commits into
NTARI-RAND:mainfrom
nirmalhk7:nirmal/agrinet-test-coverage

Conversation

@nirmalhk7

@nirmalhk7 nirmalhk7 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds 37 tests for inventory handlers and translation behavior. Inventory coverage exercises model defaults, stock and price updates, external imports, missing configuration, and storage/network failures. Translation coverage exercises locale fallback, dialect inheritance, PING terminology, supplied variables, repeated placeholders, and unresolved-placeholder warnings.

External-import coverage includes a partial record, checks applied defaults and omitted unknown fields, and verifies that supplied fields survive normalization. The fetch assertion checks the destination URL while allowing request options such as timeouts. Separate translation cases cover a normal Oaxaca message and repeated placeholders without requiring caller-supplied values to be expanded again.

Rebased onto upstream main at 9373310. The PR changes only the two test files; production code, dependencies, test runners, and quality checks are unchanged relative to that base. Plain cd backend && npm test discovers both suites.

Validation rechecked on November 13, 2026, at ac34b438a8746a1135a7c7271d29bb6b41faa92a:

  • The full backend suite passed: 61 Node tests and 83 Jest tests, including these 37 tests.
  • Both added suites pass independently: cd backend && node node_modules/jest/bin/jest.js --config jest.config.js --runInBand --runTestsByPath __tests__/inventory_controller.test.js __tests__/translation.test.js runs all 37 tests. Scoped coverage was remeasured for the inventory controller/model and translation utility.
  • Inventory controller/model coverage is 100% for statements, branches, functions, and lines. Translation coverage is 100% for statements, functions, and lines, with 94.73% branch coverage.
  • Checks against deliberate regressions reject bypassing model normalization, replacing only the first repeated placeholder, and fetching the wrong URL. Compatible single-pass substitution and fetch-timeout controls pass.
  • JavaScript syntax checks for both test files, ./scripts/list-hardcoded-urls.sh and git diff --check 9373310afff24ca3294367e6308702446876bc2a...HEAD pass.
  • Both PR commits carry DCO sign-offs, and the hosted DCO check passes.

The earlier review's requested rebase, placeholder-test correction and description refresh are addressed. The import-normalization fixture and timeout-compatible fetch assertion also address its optional suggestions. Deliberate-regression and compatibility controls were rerun only in an isolated temporary copy; production code was not changed.

The PR is non-draft, conflict-free and ready for independent human review. Hosted CodeQL and hardcoded-URL checks await maintainer authorization. Local passing results do not replace those gates.

The contribution follows the fork/PR and per-commit DCO requirements in NTARI's Bug-Build-Test guide. This test-only change alters no conformance binding and makes no live AWS, external inventory-service or deployment verification claim. The author must not merge the PR.

@nirmalhk7
nirmalhk7 force-pushed the nirmal/agrinet-test-coverage branch from fe251ba to 782f804 Compare October 24, 2026 19:32
@nirmalhk7
nirmalhk7 marked this pull request as ready for review November 5, 2026 00:34

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for these, Nirmal. They're real tests: only the DynamoDB client and axios are mocked, and the inventory controller, model and translation code all run for real, with no network or AWS access.

What I checked: commit 782f804, against current main (9373310). I read both files and traced every test against main's code. I did not run them: our review setup doesn't execute PR code locally, and no Agrinet CI job runs unit tests (the hardcoded-URL check did run and pass). From reading, all 36 should pass on main. Both files go to Jest through backend/scripts/test-files.js, so plain npm test picks them up, and they don't overlap any open PR.

I also checked that the assertions catch real breaks. Each of these would fail at least one test:

  • dropping || [] at inventory_controller.js:9
  • changing the 201
  • loosening the !== undefined checks
  • removing the Array.isArray guard or the missing-URL guard
  • dropping toLowerCase() or dialect merging in translation.js

Nicely done.

Needed before merge:

  1. Please rebase onto main and push. The branch predates codeql.yml on main, so CodeQL has never run on this PR, and main's ruleset requires a CodeQL result. After the push a maintainer will approve the fork runs. Reviews have to come after that push.
  2. In the same push, please change backend/tests/translation.test.js:40-45. The test only gets a repeated placeholder by passing region: '{listingTerm}', which the later listingTerm variable then expands. That pins re-expansion of a caller-supplied value, and a safer single-pass replace in translateMessage (translation.js:103-105 on main) would fail it. No real template repeats a placeholder. Suggestion:
    • use region: 'Oaxaca' and expect '📈 High listing volume detected in Oaxaca – over 100 active listings.'
    • test the g flag directly with expect(translateMessage('{a} and {a}', 'en', { a: 'x' })).toBe('x and x')

Optional:
3. inventory_controller.test.js:96-109 still passes if syncExternal stops calling createInventoryItem (controller:89), because each feed item already has all 12 model fields. Adding an item like { id: 'kale', name: 'Kale', note: 'x' }, expecting defaults applied and note dropped, would cover it. Bump imported to 3 and adjust the put expectation.
4. test:106 uses toHaveBeenCalledWith(url), which requires exactly one argument, so it would fail if someone later adds a timeout to axios.get. Checking axios.get.mock.calls[0][0] would be looser.
5. The description says npm test won't pick these files up. Since cf9299f (your #183) it does. The coverage numbers were also measured on the old base, and translation.js has changed since. Please update the description after rebasing, since it may become the squash commit message.

Note, no change needed: test:58, 72-77 and 87-92 pin the current DynamoDB params: create is an unconditional put, and the stock/price updates have no existence check. That's fine for tests that record current behaviour. We read them as describing it, not endorsing it, and may tighten it later.

Clean single commit with a matching sign-off, and no new dependencies. Thanks again.

Signed-off-by: Nirmal Khedkar <nirmalhk7@gmail.com>
Signed-off-by: Nirmal Khedkar <nirmalhk7@gmail.com>
@nirmalhk7
nirmalhk7 force-pushed the nirmal/agrinet-test-coverage branch from 782f804 to ac34b43 Compare November 6, 2026 17:59
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.

2 participants