From 55f8fbe1137829e6f27955a8cbeda4e8a78d8f2a Mon Sep 17 00:00:00 2001 From: lingniu Date: Thu, 16 Jul 2026 17:37:29 +0800 Subject: [PATCH] fix(operations): stabilize empty policy audit --- .../internal/platform/source_policy_store.go | 2 +- .../internal/platform/source_policy_test.go | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 vehicle-data-platform/apps/api/internal/platform/source_policy_test.go diff --git a/vehicle-data-platform/apps/api/internal/platform/source_policy_store.go b/vehicle-data-platform/apps/api/internal/platform/source_policy_store.go index cb7599aa..fc232022 100644 --- a/vehicle-data-platform/apps/api/internal/platform/source_policy_store.go +++ b/vehicle-data-platform/apps/api/internal/platform/source_policy_store.go @@ -32,7 +32,7 @@ ORDER BY protocol, source_key`, vin) } func (s *ProductionStore) VehicleSourcePolicy(ctx context.Context, vin string) (VehicleSourcePolicyConfig, error) { - config := VehicleSourcePolicyConfig{VIN: vin, Version: 1, UpdatedBy: "system"} + config := VehicleSourcePolicyConfig{VIN: vin, Version: 1, UpdatedBy: "system", Audit: []VehicleSourcePolicyAudit{}} var updatedAt sql.NullString err := s.db.QueryRowContext(ctx, `SELECT version, updated_by, DATE_FORMAT(updated_at, '%Y-%m-%d %H:%i:%s') diff --git a/vehicle-data-platform/apps/api/internal/platform/source_policy_test.go b/vehicle-data-platform/apps/api/internal/platform/source_policy_test.go new file mode 100644 index 00000000..1315dd8d --- /dev/null +++ b/vehicle-data-platform/apps/api/internal/platform/source_policy_test.go @@ -0,0 +1,43 @@ +package platform + +import ( + "bytes" + "context" + "encoding/json" + "testing" + + "github.com/DATA-DOG/go-sqlmock" +) + +func TestProductionVehicleSourcePolicyUsesStableEmptyAuditArray(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatal(err) + } + defer db.Close() + store := NewProductionStore(db, nil, "") + mock.ExpectQuery(`SELECT version, updated_by`).WithArgs("VIN001"). + WillReturnRows(sqlmock.NewRows([]string{"version", "updated_by", "updated_at"})) + mock.ExpectQuery(`SELECT version, protocol, source_ref`).WithArgs("VIN001"). + WillReturnRows(sqlmock.NewRows([]string{"version", "protocol", "source_ref", "source_label", "actor", "changed_at", "summary"})) + config, err := store.VehicleSourcePolicy(context.Background(), "VIN001") + if err != nil { + t.Fatal(err) + } + if config.Audit == nil || len(config.Audit) != 0 { + t.Fatalf("empty audit must be a stable slice: %#v", config.Audit) + } + encoded, err := json.Marshal(config) + if err != nil { + t.Fatal(err) + } + if string(encoded) == "" || !json.Valid(encoded) { + t.Fatalf("policy JSON is invalid: %s", encoded) + } + if !bytes.Contains(encoded, []byte(`"audit":[]`)) { + t.Fatalf("empty audit encoded as null: %s", encoded) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatal(err) + } +}