fix(operations): stabilize empty policy audit

This commit is contained in:
lingniu
2026-07-16 17:37:29 +08:00
parent 96cad7eef7
commit 55f8fbe113
2 changed files with 44 additions and 1 deletions

View File

@@ -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')

View File

@@ -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)
}
}