Kaynağa Gözat

Inventory: no false weight mismatch on a group of weighed spools

- The collapsed group row compared the first spool's scale reading
  with the weight of the whole group, so every weighed group showed
  a mismatch, and Sync re-synced only the first spool.
- The group row now carries no scale reading; each spool in the
  expanded group keeps its own weight check.
maziggy 6 gün önce
ebeveyn
işleme
b211df220e

+ 44 - 1
frontend/src/__tests__/pages/InventoryPageGroupRestore.test.tsx

@@ -2,7 +2,7 @@
  * Restore on an archived spool inside an expanded group row of the table.
  * The group row passed a no-op to its members, so the button did nothing.
  */
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
 import { screen, waitFor, fireEvent } from '@testing-library/react';
 import { render } from '../utils';
 import InventoryPageRouter from '../../pages/InventoryPage';
@@ -145,3 +145,46 @@ describe('InventoryPage — restore inside a group row', () => {
     expect([2, 3]).toContain(restored[0]);
   });
 });
+
+describe('InventoryPage — weight check on a group row', () => {
+  beforeEach(() => {
+    // Group on, and the (hidden by default) Weight Check column shown.
+    vi.mocked(localStorage.getItem).mockImplementation((key: string) => {
+      if (key === 'bambuddy-inventory-group') return 'true';
+      if (key === 'bambuddy-inventory-columns') {
+        return JSON.stringify([{ id: 'weight_check', label: 'Weight Check', visible: true }]);
+      }
+      return null;
+    });
+    // Two identical unopened spools, each weighed at exactly its own weight
+    // (1000 g filament + 250 g core).
+    const weighed = { ...archivedSpool, archived_at: null, weight_used: 0, last_scale_weight: 1250 };
+    server.use(
+      http.get('/api/v1/settings/', () => HttpResponse.json(baseSettings)),
+      http.get('/api/v1/inventory/spools', () =>
+        HttpResponse.json([
+          { ...weighed, id: 2 },
+          { ...weighed, id: 3 },
+        ]),
+      ),
+      http.get('/api/v1/inventory/assignments', () => HttpResponse.json([])),
+      http.get('/api/v1/spoolman/settings', () => HttpResponse.json({ spoolman_enabled: 'false' })),
+    );
+  });
+
+  afterEach(() => {
+    vi.mocked(localStorage.getItem).mockReset();
+  });
+
+  it('shows no mismatch or Sync on the group row; each spool keeps its own check', async () => {
+    render(<InventoryPageRouter />);
+
+    const header = await screen.findByText('#2, #3');
+    // The first spool's 1250 g against the group's 2500 g read as a mismatch.
+    expect(screen.queryByTitle(/mismatch!/)).not.toBeInTheDocument();
+    expect(screen.queryByTitle('Sync: trust scale weight and reset tracking')).not.toBeInTheDocument();
+
+    fireEvent.click(header.closest('tr')!);
+    expect(await screen.findAllByTitle(/within tolerance/)).toHaveLength(2);
+  });
+});

+ 11 - 0
frontend/src/__tests__/pages/InventoryPageGrouping.test.ts

@@ -299,6 +299,17 @@ describe('computeDisplayItems', () => {
 });
 
 describe('aggregateGroupSpool (#1368)', () => {
+  it('carries no scale reading, so the header shows no weight check', () => {
+    // The first member's reading against the group's summed weight was always
+    // a mismatch, and Sync then re-synced only that member.
+    const agg = aggregateGroupSpool([
+      makeSpool({ id: 1, last_scale_weight: 1250, last_weighed_at: '2026-11-01T10:00:00Z' }),
+      makeSpool({ id: 2, last_scale_weight: 1250, last_weighed_at: '2026-11-01T10:05:00Z' }),
+    ]);
+    expect(agg.last_scale_weight).toBeNull();
+    expect(agg.last_weighed_at).toBeNull();
+  });
+
   it('sums label_weight, weight_used and core_weight across members', () => {
     const agg = aggregateGroupSpool([
       makeSpool({ id: 1, label_weight: 1000, weight_used: 200, core_weight: 250 }),

+ 5 - 0
frontend/src/utils/inventoryGrouping.ts

@@ -5,6 +5,9 @@ import type { InventorySpool } from '../api/client';
 // members so the header shows group totals (#1368), while identity fields are
 // carried from the first member — all members share them, they are the group
 // key. The expanded per-spool rows keep using the real per-member spools.
+// A scale reading belongs to one spool, so the header has none: compared with
+// the summed weight, the first member's reading made every weighed group show
+// a mismatch, and its Sync button re-synced only that first spool.
 export function aggregateGroupSpool(spools: InventorySpool[]): InventorySpool {
   const base = spools[0];
   let labelWeight = 0;
@@ -20,5 +23,7 @@ export function aggregateGroupSpool(spools: InventorySpool[]): InventorySpool {
     label_weight: labelWeight,
     weight_used: weightUsed,
     core_weight: coreWeight,
+    last_scale_weight: null,
+    last_weighed_at: null,
   };
 }