Every id in the schema is a uuid and stays one. What was wrong was putting one in front of a person: RecordVisit named every new customer 'Visitor ' || left(id::text, 8), so the arrivals feed, the shop PC and the mobile app all read "Visitor 3446ec35" - the string a shop assistant reads to a colleague and types into a search box. label is a stored column staff can overwrite and SearchVisitors matches on, so formatting around it in a front end would have left the data wrong on three surfaces. Migration 012 adds a per-client visitors.number, taken from a counter on clients with UPDATE ... RETURNING inside the visit transaction. Per client rather than global: a global sequence would tell any customer who signs up how many people the whole platform has ever seen, from their own first visitor number. The backfill numbers existing rows by first_seen_at and relabels only the eight-hex pattern the old statement produced, so a human-typed name is never overwritten. Three of the four things anyone addresses by URL already had a human name and the API simply refused it - a site has a slug, a camera has the id the engine knows it by. refs.go accepts either form anywhere an id is taken; a uuid resolves with no lookup, so every URL a client already stored keeps working. - An ambiguous camera name resolves to nothing, never to a guess: two shops may each have an "Office1" and acting on the first row would edit the wrong shop's camera. - 404 on a path, 400 on a query filter. /api/visits answered fine and it was the filter that was wrong. - site and site_id are both accepted everywhere now. They differed per endpoint, and an unknown query parameter is silently ignored, so getting it the wrong way round returned the whole estate. - The search matches V-13, which is what the product now shows. Two bugs found by running it rather than testing it: - 'Visitor ' || $2::text beside number = $2 makes Postgres deduce two types for one parameter and refuse the insert. It compiled and passed every in-memory test; the first real database rejected it, along with the existing face tests that share the path. - The fallback avatar said "V1" for Visitor 13, Visitor 10 and Visitor 15 alike, and read as the V-1 reference for a fourth person. It shows the number now. The prop is customerRef, not ref - React reserves that name and it would never have arrived. Verified on the live database and through the running API: 13 hex labels became Visitor 1-13 in first-seen order, two typed names left alone, and the same customer reachable by uuid, V-13 and 13. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HViLj9gYNRtSr7YVZmW5sn
111 lines
4.0 KiB
Go
111 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestVisitorRefRoundTrips(t *testing.T) {
|
|
for _, n := range []int64{1, 42, 999999} {
|
|
ref := VisitorRef(n)
|
|
got, ok := ParseVisitorRef(ref)
|
|
if !ok || got != n {
|
|
t.Fatalf("VisitorRef(%d) = %q, parsed back as (%d, %v)", n, ref, got, ok)
|
|
}
|
|
}
|
|
if got := VisitorRef(0); got != "" {
|
|
t.Fatalf("an unnumbered visitor must render as no reference, got %q", got)
|
|
}
|
|
}
|
|
|
|
// A shop assistant reading "Visitor 42" off a screen types 42. Refusing that
|
|
// teaches them the field is unreliable, so bare digits are accepted - and there
|
|
// is nothing for them to collide with, because a uuid is checked first and is
|
|
// never all digits.
|
|
func TestVisitorRefAcceptsWhatSomebodyWouldActuallyType(t *testing.T) {
|
|
for _, in := range []string{"V-42", "v-42", "42", " V-42 "} {
|
|
n, ok := ParseVisitorRef(in)
|
|
if !ok || n != 42 {
|
|
t.Fatalf("ParseVisitorRef(%q) = (%d, %v), want 42", in, n, ok)
|
|
}
|
|
}
|
|
for _, in := range []string{"", "V-", "V-0", "-1", "V-x", "abc", "4 2",
|
|
"3446ec35-2c1f-4c8e-9a77-0d1e2f3a4b5c"} {
|
|
if _, ok := ParseVisitorRef(in); ok {
|
|
t.Fatalf("ParseVisitorRef(%q) accepted a reference it should not", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLooksLikeNameRejectsJunkBeforeItReachesSQL(t *testing.T) {
|
|
for _, ok := range []string{"chennai", "Office1", "cam_2", "a.b-c"} {
|
|
if !looksLikeName(ok) {
|
|
t.Fatalf("%q should be a usable name", ok)
|
|
}
|
|
}
|
|
for _, bad := range []string{"", "a b", "a/b", "a'b", "../etc", "%", string(make([]byte, 65))} {
|
|
if looksLikeName(bad) {
|
|
t.Fatalf("%q should not reach a query", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The point of the whole scheme: a customer is addressable by the number staff
|
|
// read on screen, and the uuid that clients already stored keeps working.
|
|
func TestACustomerIsReachableByNumberAndByUUID(t *testing.T) {
|
|
const id = "3446ec35-2c1f-4c8e-9a77-0d1e2f3a4b5c"
|
|
srv, fs := newServer(t)
|
|
seedUser(fs)
|
|
fs.visitors = []Customer{{ID: id, Ref: "V-42", Label: "Visitor 42"}}
|
|
fs.history = []VisitRow{{Site: "Chennai"}}
|
|
tok := login(t, srv, "manager@acme.com", "correct horse battery").Token
|
|
|
|
for _, path := range []string{"/api/visitors/" + id + "/history",
|
|
"/api/visitors/V-42/history", "/api/visitors/42/history"} {
|
|
rec := do(t, srv, http.MethodGet, path, tok, nil)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET %s = %d, want 200", path, rec.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A reference that names nobody is 404 on a path, and a filter that names no
|
|
// shop is 400 on a query string. The difference matters: `/api/visits` answered
|
|
// fine and what was wrong was the filter, so a 404 there would send somebody
|
|
// looking for a missing arrivals feed.
|
|
func TestAnUnknownReferenceIs404OnAPathAnd400OnAFilter(t *testing.T) {
|
|
srv, fs := newServer(t)
|
|
seedUser(fs)
|
|
tok := login(t, srv, "manager@acme.com", "correct horse battery").Token
|
|
|
|
rec := do(t, srv, http.MethodGet, "/api/visitors/V-999/history", tok, nil)
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("unknown customer = %d, want 404", rec.Code)
|
|
}
|
|
rec = do(t, srv, http.MethodGet, "/api/visits?site=nowhere", tok, nil)
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("unknown shop filter = %d, want 400", rec.Code)
|
|
}
|
|
}
|
|
|
|
// Reports have always taken `site` and the arrivals feed `site_id`. Both work
|
|
// everywhere now, because an unknown query parameter is silently ignored - so
|
|
// getting it the wrong way round returned the whole estate rather than an
|
|
// error, which is a wrong number nobody would question.
|
|
func TestBothSiteParameterNamesAreAccepted(t *testing.T) {
|
|
srv, fs := newServer(t)
|
|
seedUser(fs)
|
|
fs.sites = []SiteHealth{{SiteID: "11111111-1111-4111-8111-111111111111", Slug: "chennai"}}
|
|
tok := login(t, srv, "manager@acme.com", "correct horse battery").Token
|
|
|
|
for _, q := range []string{"site=chennai", "site_id=chennai"} {
|
|
fs.arrivalQ = ArrivalQuery{}
|
|
if rec := do(t, srv, http.MethodGet, "/api/visits?"+q, tok, nil); rec.Code != http.StatusOK {
|
|
t.Fatalf("GET /api/visits?%s = %d", q, rec.Code)
|
|
}
|
|
if got := fs.arrivalQ.SiteID; got != "11111111-1111-4111-8111-111111111111" {
|
|
t.Fatalf("%s resolved to %q", q, got)
|
|
}
|
|
}
|
|
}
|