fix: increment/decrement use delta endpoint instead of full stock update #6

Closed
admin-backup wants to merge 3 commits from copilot/fix-stock-delta-addition-save into main
pull from: copilot/fix-stock-delta-addition-save
admin-backup commented 2026-03-25 00:40:39 +01:00 (Migrated from 172.24.0.1:8928)

increment and decrement were mutating current_quantity locally then firing a debounced updateStock (PUT /stock), bypassing the dedicated delta endpoint entirely and risking overwriting concurrent edits with stale local state.

Changes

Bug fix — useStockManager.ts

  • Destructure adjustStockQuantity from useStocks()
  • Replace local mutation + queueDebouncedSave with adjustStockQuantity(stock.id, ±1) → POST /stock/:id/delta
  • Add proper saving → saved/error save-state transitions with feedback timers (consistent with save())
// Before
stock.current_quantity = normalizeQuantity(stock.current_quantity + 1);
queueDebouncedSave(stock); // PUT /stock — wrong endpoint, stale data

// After
await adjustStockQuantity(stock.id, 1); // POST /stock/:id/delta
  • Remove now-dead queueDebouncedSave and saveDebounceMs

Test infrastructure

  • Add vitest + jsdom + @vue/test-utils; wire npm test / npm run test:watch
  • backend.test.ts — all stock HTTP functions (getStocks, createStock, editStock, deleteStock, adjustStockByDelta), including error paths
  • stocks.test.ts — useStocks composable: load, add, update (with/without refresh), remove, adjust-by-delta
  • useStockManager.test.ts — useStockManager: increment, decrement, save, createNewStock, remove, state labels/classes, toggle view — 44 tests total
Original prompt

in the mobile part fix stock delta addition and save, verify each one of the endpoint and function of the app. You should probably add front test for this.


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

`increment` and `decrement` were mutating `current_quantity` locally then firing a debounced `updateStock` (PUT `/stock`), bypassing the dedicated delta endpoint entirely and risking overwriting concurrent edits with stale local state. ## Changes ### Bug fix — `useStockManager.ts` - Destructure `adjustStockQuantity` from `useStocks()` - Replace local mutation + `queueDebouncedSave` with `adjustStockQuantity(stock.id, ±1)` → POST `/stock/:id/delta` - Add proper `saving` → `saved`/`error` save-state transitions with feedback timers (consistent with `save()`) ```ts // Before stock.current_quantity = normalizeQuantity(stock.current_quantity + 1); queueDebouncedSave(stock); // PUT /stock — wrong endpoint, stale data // After await adjustStockQuantity(stock.id, 1); // POST /stock/:id/delta ``` - Remove now-dead `queueDebouncedSave` and `saveDebounceMs` ### Test infrastructure - Add `vitest` + `jsdom` + `@vue/test-utils`; wire `npm test` / `npm run test:watch` - **`backend.test.ts`** — all stock HTTP functions (`getStocks`, `createStock`, `editStock`, `deleteStock`, `adjustStockByDelta`), including error paths - **`stocks.test.ts`** — `useStocks` composable: load, add, update (with/without refresh), remove, adjust-by-delta - **`useStockManager.test.ts`** — `useStockManager`: increment, decrement, save, createNewStock, remove, state labels/classes, toggle view — 44 tests total <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > in the mobile part fix stock delta addition and save, verify each one of the endpoint and function of the app. You should probably add front test for this. </details> <!-- START COPILOT CODING AGENT TIPS --> --- 📱 Kick off Copilot coding agent tasks wherever you are with [GitHub Mobile](https://gh.io/cca-mobile-docs), available on iOS and Android.
admin-backup commented 2026-03-25 00:40:40 +01:00 (Migrated from 172.24.0.1:8928)

assigned to @Copilot

assigned to `@Copilot`
admin-backup commented 2026-03-25 00:40:40 +01:00 (Migrated from 172.24.0.1:8928)

assigned to @THoguet

assigned to `@THoguet`
admin-backup commented 2026-03-25 00:53:53 +01:00 (Migrated from 172.24.0.1:8928)

changed title from {-[WIP] Fix stock delta addition and save functionality-} to {+fix: increment/decrement use delta endpoint instead of full stock update+}

changed title from **{-[WIP] Fix stock delta addition and save functionality-}** to **{+fix: increment/decrement use delta endpoint instead of full stock update+}**
admin-backup commented 2026-03-25 00:53:58 +01:00 (Migrated from 172.24.0.1:8928)

requested review from @THoguet

requested review from `@THoguet`
admin-backup commented 2026-03-25 23:38:57 +01:00 (Migrated from 172.24.0.1:8928)

approved this merge request

approved this merge request
admin-backup (Migrated from 172.24.0.1:8928) approved these changes 2026-03-25 23:38:57 +01:00
admin-backup commented 2026-03-25 23:39:35 +01:00 (Migrated from 172.24.0.1:8928)

requested review from @Copilot

requested review from `@Copilot`
admin-backup commented 2026-03-25 23:44:10 +01:00 (Migrated from 172.24.0.1:8928)

pendingSaves is now dead state: the debounce queue was removed, but the map is still allocated and save() still tries to cancel a non-existent pending timeout. Please remove pendingSaves (and the related cancel logic) or reintroduce the place where timeouts are added so the state is consistent.

`pendingSaves` is now dead state: the debounce queue was removed, but the map is still allocated and `save()` still tries to cancel a non-existent pending timeout. Please remove `pendingSaves` (and the related cancel logic) or reintroduce the place where timeouts are added so the state is consistent.
admin-backup commented 2026-03-25 23:44:11 +01:00 (Migrated from 172.24.0.1:8928)

These tests call useStockManager() directly, but the composable registers onMounted/onBeforeUnmount hooks. Calling it outside a component setup produces Vue warnings and means the mount/unmount behavior (like loadStocks() on mount and timer cleanup) is never exercised. Consider mounting a minimal test component (via @vue/test-utils) that invokes the composable in setup(), and assert the lifecycle behavior there.

These tests call `useStockManager()` directly, but the composable registers `onMounted`/`onBeforeUnmount` hooks. Calling it outside a component setup produces Vue warnings and means the mount/unmount behavior (like `loadStocks()` on mount and timer cleanup) is never exercised. Consider mounting a minimal test component (via `@vue/test-utils`) that invokes the composable in `setup()`, and assert the lifecycle behavior there.
admin-backup commented 2026-03-25 23:44:11 +01:00 (Migrated from 172.24.0.1:8928)

This suite overwrites globalThis.fetch with vi.fn() but never restores the original implementation. That can leak into other test files and cause unexpected failures. Save the original fetch before mocking and restore it in afterEach/afterAll.

This suite overwrites `globalThis.fetch` with `vi.fn()` but never restores the original implementation. That can leak into other test files and cause unexpected failures. Save the original `fetch` before mocking and restore it in `afterEach`/`afterAll`.
admin-backup commented 2026-03-25 23:44:11 +01:00 (Migrated from 172.24.0.1:8928)

useStocks keeps stocks/loading/error as module-level refs, so state persists across tests. This file only clears mock calls, not the composable state, which can make tests order-dependent. Reset the module (e.g. vi.resetModules() + re-import) or explicitly reset stocks.value, loading.value, and error.value in beforeEach.

`useStocks` keeps `stocks/loading/error` as module-level refs, so state persists across tests. This file only clears mock calls, not the composable state, which can make tests order-dependent. Reset the module (e.g. `vi.resetModules()` + re-import) or explicitly reset `stocks.value`, `loading.value`, and `error.value` in `beforeEach`.
admin-backup commented 2026-03-25 23:44:11 +01:00 (Migrated from 172.24.0.1:8928)

This repo uses Bun in CI (bun install), and already has mobile/bun.lock. Adding package-lock.json introduces a second lockfile that can drift from bun.lock (and bun.lock in this PR doesn’t include the new devDependencies). Please standardize on a single lockfile: either update/commit bun.lock and remove package-lock.json, or switch CI/package manager to npm and keep package-lock.json.

This repo uses Bun in CI (`bun install`), and already has `mobile/bun.lock`. Adding `package-lock.json` introduces a second lockfile that can drift from `bun.lock` (and `bun.lock` in this PR doesn’t include the new devDependencies). Please standardize on a single lockfile: either update/commit `bun.lock` and remove `package-lock.json`, or switch CI/package manager to npm and keep `package-lock.json`.
admin-backup commented 2026-03-25 23:44:11 +01:00 (Migrated from 172.24.0.1:8928)

Review: Commented

Pull request overview

This PR fixes stock increment/decrement to use the backend delta endpoint (POST /stock/:id/delta) instead of mutating local quantity and doing a full stock PUT, and adds a Vitest-based unit test setup for the mobile frontend.

Changes:

  • Update useStockManager increment/decrement to call adjustStockQuantity(stock.id, ±1) and manage per-line save state (saving → saved/error).
  • Add Vitest + jsdom + Vue test tooling and unit tests for backend stock HTTP helpers, the useStocks composable, and useStockManager.
  • Add npm test / npm run test:watch scripts.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
mobile/vitest.config.ts Adds Vitest configuration (jsdom, globals) for frontend tests.
mobile/src/composables/useStockManager.ts Switches increment/decrement to delta endpoint via adjustStockQuantity; removes debounced PUT path.
mobile/src/tests/useStockManager.test.ts Adds unit tests for useStockManager behavior (increment/decrement/save/etc.).
mobile/src/tests/stocks.test.ts Adds unit tests for useStocks (including adjust-by-delta).
mobile/src/tests/backend.test.ts Adds unit tests for stock HTTP functions/endpoints and error paths.
mobile/package.json Adds test scripts and devDependencies for Vitest/jsdom/test-utils.
mobile/package-lock.json Introduces npm lockfile alongside existing Bun lockfile.

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

**Review:** Commented ## Pull request overview This PR fixes stock increment/decrement to use the backend delta endpoint (POST `/stock/:id/delta`) instead of mutating local quantity and doing a full stock PUT, and adds a Vitest-based unit test setup for the mobile frontend. **Changes:** - Update `useStockManager` increment/decrement to call `adjustStockQuantity(stock.id, ±1)` and manage per-line save state (`saving` → `saved`/`error`). - Add Vitest + jsdom + Vue test tooling and unit tests for backend stock HTTP helpers, the `useStocks` composable, and `useStockManager`. - Add `npm test` / `npm run test:watch` scripts. ### Reviewed changes Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments. <details> <summary>Show a summary per file</summary> | File | Description | | ---- | ----------- | | mobile/vitest.config.ts | Adds Vitest configuration (jsdom, globals) for frontend tests. | | mobile/src/composables/useStockManager.ts | Switches increment/decrement to delta endpoint via `adjustStockQuantity`; removes debounced PUT path. | | mobile/src/__tests__/useStockManager.test.ts | Adds unit tests for `useStockManager` behavior (increment/decrement/save/etc.). | | mobile/src/__tests__/stocks.test.ts | Adds unit tests for `useStocks` (including adjust-by-delta). | | mobile/src/__tests__/backend.test.ts | Adds unit tests for stock HTTP functions/endpoints and error paths. | | mobile/package.json | Adds test scripts and devDependencies for Vitest/jsdom/test-utils. | | mobile/package-lock.json | Introduces npm lockfile alongside existing Bun lockfile. | </details> --- 💡 <a href="/THoguet/expiry-scanner/new/main?filename=.github/instructions/*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
admin-backup commented 2026-03-26 18:28:48 +01:00 (Migrated from 172.24.0.1:8928)

@copilot

`@copilot`
admin-backup (Migrated from 172.24.0.1:8928) closed this pull request 2026-03-26 18:30:19 +01:00
admin-backup commented 2026-03-26 18:30:41 +01:00 (Migrated from 172.24.0.1:8928)

requested review from @THoguet

requested review from `@THoguet`

Preserved source assignment/reviewer metadata. Historical accounts stay inactive.

Source requested reviewers: admin-backup (GitLab user 3).

The linked history record retains original IDs and source dates. No historical identity was merged into nessar or granted new authentication permissions.

<!-- gitlab-to-gitea-assignment:16:MergeRequest:5 --> Preserved source assignment/reviewer metadata. Historical accounts stay inactive. Source requested reviewers: admin-backup (GitLab user 3). The linked history record retains original IDs and source dates. No historical identity was merged into nessar or granted new authentication permissions.

Preserved GitLab !5 history: source attribution, exact timestamps, discussion grouping and review positions. Historical diff snapshots: 1; visible source notes: 14.

gitlab-16-mergerequest-5.md (SHA-256 af59a4b487604463b27e6ef7bd5e40ce6abf33e0c52dc98cbd6bad00f94cf4b2)

gitlab-16-mergerequest-5.json (SHA-256 b3f69677ab8d0653193b9c0577f2e3dc4bd9cf4be86c98cfd147c96d50d81634)

Old CI job execution and reports are omitted. Original source files and rollback backups remain protected.

<!-- gitlab-to-gitea-history:16:MergeRequest:5 --> Preserved GitLab !5 history: source attribution, exact timestamps, discussion grouping and review positions. Historical diff snapshots: 1; visible source notes: 14. [gitlab-16-mergerequest-5.md](https://gitea.nessar.fr/attachments/8057f08d-e21d-43bd-b36e-a183933afd8d) (SHA-256 `af59a4b487604463b27e6ef7bd5e40ce6abf33e0c52dc98cbd6bad00f94cf4b2`) [gitlab-16-mergerequest-5.json](https://gitea.nessar.fr/attachments/05185fb2-8790-4b09-8dd2-e2950724b063) (SHA-256 `b3f69677ab8d0653193b9c0577f2e3dc4bd9cf4be86c98cfd147c96d50d81634`) Old CI job execution and reports are omitted. Original source files and rollback backups remain protected.

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nessar/expiry-scanner#6