File size: 6,483 Bytes
e48a2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package pool

import (
	"encoding/json"
	"math"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/misakano7545/workbuddy2api-panel/internal/auth"
)

// 移植上游 PR #104:小数积分保留 + 零成本观测可信标记。

func exactCreditsForTest(p *Pool, uid string) float64 {
	p.mu.RLock()
	defer p.mu.RUnlock()
	return p.byUID[uid].exactCredits()
}

// TestFractionalCreditChargesPersistAndAuthoritativeUpdatesResetRemainder
// 小数扣费不丢:15×0.1 后精确余额 99.5(整数口径显示 100);跨重启保号;
// 权威余额整写(SetCredits/SetCreditsDetailed/ReenableIfCredits)清零余量。
func TestFractionalCreditChargesPersistAndAuthoritativeUpdatesResetRemainder(t *testing.T) {
	statePath := filepath.Join(t.TempDir(), "state.json")
	p := New(statePath)
	defer p.Close()
	p.Add(&auth.Auth{UID: "u1"})
	p.SetCredits("u1", 101, 0)
	for i := 0; i < 15; i++ {
		p.NoteModelCost("u1", "fractional", 0.1, 1000)
	}
	if got := exactCreditsForTest(p, "u1"); got != 99.5 {
		t.Fatalf("exact balance after 15 x 0.1 charges = %v, want 99.5", got)
	}
	if status, ok := p.Status("u1"); !ok || status.Credits != 100 {
		t.Fatalf("integer external balance should remain rounded: status=%+v ok=%v", status, ok)
	}
	p.Flush()
	p.Close()

	restored := New(statePath)
	defer restored.Close()
	restored.Add(&auth.Auth{UID: "u1"})
	if got := exactCreditsForTest(restored, "u1"); got != 99.5 {
		t.Fatalf("restored exact balance = %v, want 99.5", got)
	}

	restored.SetCredits("u1", 200, 0)
	if got := exactCreditsForTest(restored, "u1"); got != 200 {
		t.Fatalf("SetCredits must reset fractional remainder, got %v", got)
	}
	restored.NoteModelCost("u1", "fractional", 0.1, 1000)
	if got := exactCreditsForTest(restored, "u1"); got != 199.9 {
		t.Fatalf("fractional charge after SetCredits = %v, want 199.9", got)
	}

	restored.SetCreditsDetailed("u1", 70, 70, 0, time.Time{}, 0)
	if got := exactCreditsForTest(restored, "u1"); got != 70 {
		t.Fatalf("SetCreditsDetailed must reset fractional remainder, got %v", got)
	}
	restored.NoteModelCost("u1", "fractional", 0.1, 1000)
	restored.ReenableIfCredits("u1", 50, 0)
	if got := exactCreditsForTest(restored, "u1"); got != 50 {
		t.Fatalf("ReenableIfCredits must reset fractional remainder, got %v", got)
	}
}

// TestConfirmedFreeObservationPersistsVerification 确认免费(credit 显式=0)
// 的观测带可信标记落盘,并跨重启恢复为 tier 0 证据。
func TestConfirmedFreeObservationPersistsVerification(t *testing.T) {
	statePath := filepath.Join(t.TempDir(), "state.json")
	p := New(statePath)
	defer p.Close()
	p.Add(&auth.Auth{UID: "u1"})
	p.SetCredits("u1", 100, 0)
	p.NoteModelCost("u1", "free", 0, 1000)
	p.Flush()
	p.Close()

	raw, err := os.ReadFile(statePath)
	if err != nil {
		t.Fatal(err)
	}
	var saved stateFile
	if err := json.Unmarshal(raw, &saved); err != nil {
		t.Fatal(err)
	}
	if !saved.Accounts["u1"].ModelCosts["free"].CreditVerified {
		t.Fatal("new zero-cost observation must be persisted with verified credit provenance")
	}

	restored := New(statePath)
	defer restored.Close()
	restored.Add(&auth.Auth{UID: "u1"})
	status, ok := restored.Status("u1")
	if !ok || len(status.ModelCosts) != 1 || status.ModelCosts[0].CostPer1k != 0 {
		t.Fatalf("verified free observation should survive restart, status=%+v ok=%v", status, ok)
	}
}

// TestCreditFloorUsesExactFractionalBalance 保底判定看精确余额:整数口径还等于
// 保底线、实际已跌破(101 → 100.9)时,付费模型必须被拦住。
func TestCreditFloorUsesExactFractionalBalance(t *testing.T) {
	withNoPickGap(t)
	p := New("")
	defer p.Close()
	p.SetCostExploreInterval(0)
	p.SetCreditFloor(101)
	p.Add(&auth.Auth{UID: "u1"})
	p.SetCredits("u1", 101, 0)
	p.NoteModelCost("u1", "paid", 0.1, 1000)

	if status, ok := p.Status("u1"); !ok || status.Credits != 101 {
		t.Fatalf("legacy integer field should still show 101: status=%+v ok=%v", status, ok)
	}
	if got := exactCreditsForTest(p, "u1"); got != 100.9 {
		t.Fatalf("exact balance = %v, want 100.9", got)
	}
	if a := p.PickExcludingForRealm(nil, "paid", ""); a != nil {
		t.Fatalf("exact balance below floor must block the paid model even while integer field equals floor, got %v", a.UID)
	}
}

// TestLegacyZeroCostMigratesButVerifiedFreeAndLegacyPaidRemain 旧版「无标记的零价
// 观测」一次性迁移清除(脏免费证据);已验证免费与旧正价记录保留,其余账号状态不动。
func TestLegacyZeroCostMigratesButVerifiedFreeAndLegacyPaidRemain(t *testing.T) {
	statePath := filepath.Join(t.TempDir(), "state.json")
	now := time.Now()
	sf := stateFile{Accounts: map[string]stateAccount{
		"old-zero": {
			Credits:    321,
			TokenUsage: TokenUsage{RequestCount: 7, TotalTokens: 44},
			ModelCosts: map[string]stateModelCost{
				"legacy-free": {CostPer1k: 0, LastSeen: now, Samples: 3},
			},
		},
		"confirmed-zero": {
			Credits: 222,
			ModelCosts: map[string]stateModelCost{
				"verified-free": {CostPer1k: 0, LastSeen: now, Samples: 2, CreditVerified: true},
			},
		},
		"old-paid": {
			Credits: 111,
			ModelCosts: map[string]stateModelCost{
				"legacy-paid": {CostPer1k: 1.25, LastSeen: now, Samples: 5},
			},
		},
	}}
	raw, err := json.Marshal(sf)
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(statePath, raw, 0o600); err != nil {
		t.Fatal(err)
	}
	p := New(statePath)
	defer p.Close()
	for _, uid := range []string{"old-zero", "confirmed-zero", "old-paid"} {
		p.Add(&auth.Auth{UID: uid})
	}

	oldZero, ok := p.Status("old-zero")
	if !ok || oldZero.Credits != 321 || oldZero.TokenUsage.RequestCount != 7 || oldZero.TokenUsage.TotalTokens != 44 {
		t.Fatalf("migration must preserve other account state: status=%+v ok=%v", oldZero, ok)
	}
	if len(oldZero.ModelCosts) != 0 {
		t.Fatalf("legacy zero without verified source must become unknown, got %+v", oldZero.ModelCosts)
	}
	confirmedZero, ok := p.Status("confirmed-zero")
	if !ok || len(confirmedZero.ModelCosts) != 1 || confirmedZero.ModelCosts[0].CostPer1k != 0 {
		t.Fatalf("verified free observation should restore, got %+v ok=%v", confirmedZero, ok)
	}
	oldPaid, ok := p.Status("old-paid")
	if !ok || len(oldPaid.ModelCosts) != 1 || math.Abs(oldPaid.ModelCosts[0].CostPer1k-1.25) > 1e-12 {
		t.Fatalf("legacy positive observation should restore, got %+v ok=%v", oldPaid, ok)
	}
	if !p.dirty.Load() {
		t.Fatal("dropping legacy zero observations should dirty state for one-time migration persistence")
	}
}