package samawards import ( "encoding/json" "testing" "github.com/shopspring/decimal" ) // fixture is one real shape awardSummary element, matching the live SAM Contract Awards response // envelope (nested contractId, awardDetails, coreData), including the two distinct date formats the API // returns (dateSigned with a Z, the period of performance dates space separated with milliseconds). const fixture = `{ "contractId": { "piid": "W900KK25FA080", "modificationNumber": "P00003", "transactionNumber": "0", "subtier": { "code": "9700", "name": "DEPT OF DEFENSE" } }, "awardDetails": { "awardeeData": { "awardeeHeader": { "awardeeName": "ASK SAGE, INC." }, "awardeeUEIInformation": { "uniqueEntityId": "W9X4EWLUBAW1" } }, "dollars": { "actionObligation": 14850.0, "baseAndAllOptionsValue": 59400 }, "dates": { "dateSigned": "2025-05-07T00:00:00Z", "periodOfPerformanceStartDate": "2025-05-12 00:00:00.000", "currentCompletionDate": "2026-05-11 00:00:00.000" } }, "coreData": { "federalOrganization": { "contractingInformation": { "contractingDepartment": { "name": "DEPT OF DEFENSE" } } }, "productOrServiceInformation": { "principalNaics": [ { "code": "541512", "description": "Computer Systems Design Services" } ] }, "preferencePrograms": { "typeOfSetAside": { "name": "8(a) Sole Source" } }, "contractData": { "descriptionOfContractRequirement": "Cyber engineering support" } } }` func TestNormalize(t *testing.T) { a, ok := Normalize(json.RawMessage(fixture)) if !ok { t.Fatal("Normalize returned not ok for a valid record") } if a.PIID != "W900KK25FA080" { t.Fatalf("piid = %q", a.PIID) } if a.UniqueKey != "W900KK25FA080|P00003|0" { t.Fatalf("unique key = %q", a.UniqueKey) } if a.AwardeeName != "ASK SAGE, INC." || a.AwardeeUEI != "W9X4EWLUBAW1" { t.Fatalf("incumbent wrong: %q %q", a.AwardeeName, a.AwardeeUEI) } if a.TotalValue == nil || !a.TotalValue.Equal(decimal.RequireFromString("59400")) { t.Fatalf("total value = %v", a.TotalValue) } if a.ObligatedValue == nil || !a.ObligatedValue.Equal(decimal.RequireFromString("14850")) { t.Fatalf("obligated value = %v", a.ObligatedValue) } if a.Department != "DEPT OF DEFENSE" || a.Subtier != "DEPT OF DEFENSE" { t.Fatalf("agency wrong: %q %q", a.Department, a.Subtier) } if a.NAICSCode != "541512" || a.NAICSDescription != "Computer Systems Design Services" { t.Fatalf("naics wrong: %q %q", a.NAICSCode, a.NAICSDescription) } if a.SetAside != "8(a) Sole Source" { t.Fatalf("set aside = %q", a.SetAside) } if a.Description != "Cyber engineering support" { t.Fatalf("description = %q", a.Description) } if a.DateSigned == nil || a.DateSigned.Format("2006-01-02") != "2025-05-07" { t.Fatalf("date signed = %v", a.DateSigned) } if a.POPStart == nil || a.POPStart.Format("2006-01-02") != "2025-05-12" { t.Fatalf("pop start = %v", a.POPStart) } if a.POPEnd == nil || a.POPEnd.Format("2006-01-02") != "2026-05-11" { t.Fatalf("pop end = %v", a.POPEnd) } } func TestNormalizeSkipsRecordWithoutPIID(t *testing.T) { if _, ok := Normalize(json.RawMessage(`{"contractId":{"piid":""}}`)); ok { t.Fatal("a record without a PIID must be skipped") } } func TestNormalizeStringFormNumerics(t *testing.T) { // The live API returns the dollar amounts as quoted strings, not JSON numbers, so NumericText must // decode both forms. This guards the wire mismatch that the jq probe masked. raw := `{"contractId":{"piid":"P1"},"awardDetails":{"dollars":{"actionObligation":"14850.0","baseAndAllOptionsValue":"59400"}}}` a, ok := Normalize(json.RawMessage(raw)) if !ok { t.Fatal("Normalize should accept string form dollars") } if a.TotalValue == nil || !a.TotalValue.Equal(decimal.RequireFromString("59400")) { t.Fatalf("total = %v", a.TotalValue) } if a.ObligatedValue == nil || !a.ObligatedValue.Equal(decimal.RequireFromString("14850")) { t.Fatalf("obligated = %v", a.ObligatedValue) } } func TestNumericTextInt(t *testing.T) { cases := map[string]int{`"48845"`: 48845, `48845`: 48845, `"0"`: 0, `null`: 0, `""`: 0, `"12.0"`: 12} for in, want := range cases { var n NumericText if err := json.Unmarshal([]byte(in), &n); err != nil { t.Fatalf("unmarshal %s: %v", in, err) } if n.Int() != want { t.Fatalf("Int(%s) = %d, want %d", in, n.Int(), want) } } } func TestNormalizeAbsentDollarsStayNil(t *testing.T) { a, ok := Normalize(json.RawMessage(`{"contractId":{"piid":"X1"}}`)) if !ok { t.Fatal("a record with a PIID and no dollars is still valid") } if a.TotalValue != nil || a.ObligatedValue != nil { t.Fatal("absent dollars must stay nil, never a fabricated zero") } }