bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package output
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestPrintJson_InjectNotice_Map(t *testing.T) {
|
|
origNotice := PendingNotice
|
|
PendingNotice = func() map[string]interface{} {
|
|
return map[string]interface{}{"update": "available"}
|
|
}
|
|
defer func() { PendingNotice = origNotice }()
|
|
|
|
data := map[string]interface{}{"ok": true, "data": "test"}
|
|
var buf bytes.Buffer
|
|
PrintJson(&buf, data)
|
|
|
|
var got map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatalf("failed to parse: %v", err)
|
|
}
|
|
notice, ok := got["_notice"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("expected _notice in map-based envelope")
|
|
}
|
|
if notice["update"] != "available" {
|
|
t.Errorf("expected update=available, got %v", notice["update"])
|
|
}
|
|
}
|
|
|
|
func TestPrintJson_InjectNotice_SkipsNonEnvelope(t *testing.T) {
|
|
origNotice := PendingNotice
|
|
PendingNotice = func() map[string]interface{} {
|
|
return map[string]interface{}{"update": "available"}
|
|
}
|
|
defer func() { PendingNotice = origNotice }()
|
|
|
|
// Map without "ok" key should not get _notice
|
|
data := map[string]interface{}{"name": "test"}
|
|
var buf bytes.Buffer
|
|
PrintJson(&buf, data)
|
|
|
|
var got map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatalf("failed to parse: %v", err)
|
|
}
|
|
if _, ok := got["_notice"]; ok {
|
|
t.Error("expected no _notice for non-envelope map")
|
|
}
|
|
}
|
|
|
|
func TestPrintJson_Struct_PreservesNotice(t *testing.T) {
|
|
origNotice := PendingNotice
|
|
PendingNotice = nil // no global notice
|
|
defer func() { PendingNotice = origNotice }()
|
|
|
|
// Struct with Notice already set should preserve it
|
|
env := &Envelope{
|
|
OK: true,
|
|
Identity: "user",
|
|
Data: "hello",
|
|
Notice: map[string]interface{}{"update": "set-by-caller"},
|
|
}
|
|
var buf bytes.Buffer
|
|
PrintJson(&buf, env)
|
|
|
|
var got map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatalf("failed to parse: %v", err)
|
|
}
|
|
notice, ok := got["_notice"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("expected _notice from struct field")
|
|
}
|
|
if notice["update"] != "set-by-caller" {
|
|
t.Errorf("expected update=set-by-caller, got %v", notice["update"])
|
|
}
|
|
}
|
|
|
|
func TestPrintJson_NoNotice(t *testing.T) {
|
|
origNotice := PendingNotice
|
|
PendingNotice = nil
|
|
defer func() { PendingNotice = origNotice }()
|
|
|
|
data := map[string]interface{}{"ok": true, "data": "test"}
|
|
var buf bytes.Buffer
|
|
PrintJson(&buf, data)
|
|
|
|
var got map[string]interface{}
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
|
t.Fatalf("failed to parse: %v", err)
|
|
}
|
|
if _, ok := got["_notice"]; ok {
|
|
t.Error("expected no _notice when PendingNotice is nil")
|
|
}
|
|
}
|