feat(trucks): site_id column and site/truck helpers

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-15 11:44:37 +08:00
co-authored by Claude Sonnet 5.5
parent fd7a55337f
commit 867b725262
9 changed files with 105 additions and 1 deletions
+32
View File
@@ -0,0 +1,32 @@
import { describe, it, expect } from 'vitest'
import { trucksForSite, siteForTruck } from '@/lib/report/trucks'
const trucks = [
{ id: 't1', site_id: 'A' },
{ id: 't2', site_id: 'B' },
{ id: 't3', site_id: null },
]
describe('trucksForSite', () => {
it('returns matching and unassigned trucks', () => {
expect(trucksForSite(trucks, 'A').map(t => t.id)).toEqual(['t1', 't3'])
expect(trucksForSite(trucks, 'B').map(t => t.id)).toEqual(['t2', 't3'])
})
it('returns all trucks when no site is selected', () => {
expect(trucksForSite(trucks, '').map(t => t.id)).toEqual(['t1', 't2', 't3'])
})
it('returns only unassigned trucks for a site with none linked', () => {
expect(trucksForSite(trucks, 'C').map(t => t.id)).toEqual(['t3'])
})
})
describe('siteForTruck', () => {
it('returns the truck site', () => {
expect(siteForTruck(trucks, 't2')).toBe('B')
})
it('returns null for unassigned, unknown or missing truck', () => {
expect(siteForTruck(trucks, 't3')).toBeNull()
expect(siteForTruck(trucks, 'nope')).toBeNull()
expect(siteForTruck(trucks, null)).toBeNull()
})
})
+15 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { groupSites, zonesForSite, resolveZoneToken, type ZoneRow } from '@/lib/report/zones'
import { groupSites, zonesForSite, resolveZoneToken, siteIdForToken, type ZoneRow } from '@/lib/report/zones'
const rows: ZoneRow[] = [
{ siteId: 's1', siteName: 'SC1', zoneId: 'z1', zoneName: 'Dock A', token: 'tok-a' },
@@ -44,3 +44,17 @@ describe('resolveZoneToken', () => {
expect(resolveZoneToken({ zoneToken: null, sites, zoneId: 'zz' })).toBeNull()
})
})
describe('siteIdForToken', () => {
const sites = [
{ id: 'A', name: 'Site A', zones: [{ id: 'z1', name: 'Dock', qr_code_token: 'tok-1' }] },
{ id: 'B', name: 'Site B', zones: [{ id: 'z2', name: 'Yard', qr_code_token: 'tok-2' }] },
]
it('finds the site that owns the token', () => {
expect(siteIdForToken(sites, 'tok-2')).toBe('B')
})
it('returns empty string for null or unknown token', () => {
expect(siteIdForToken(sites, null)).toBe('')
expect(siteIdForToken(sites, 'nope')).toBe('')
})
})