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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package syncctl

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/archnemix/archx-eval/relay/internal/hf"
)

// fakeRemote records every commit so the tests can assert on what was sent.
type fakeRemote struct {
	mu            sync.Mutex
	existing      map[string]string // path -> blob sha
	publishes     []publishCall
	failWith      error
	failTimes     int
	concurrent    int32
	maxConcurrent int32
	downloads     int32
}

type publishCall struct {
	summary string
	files   []hf.File
}

func newFake() *fakeRemote {
	return &fakeRemote{existing: map[string]string{}}
}

func (f *fakeRemote) List(context.Context, string) ([]hf.RemoteFile, error) {
	f.mu.Lock()
	defer f.mu.Unlock()
	var out []hf.RemoteFile
	for p, v := range f.existing {
		out = append(out, hf.RemoteFile{Path: p, Version: v})
	}
	return out, nil
}

func (f *fakeRemote) Publish(_ context.Context, summary string, files []hf.File) error {
	now := atomic.AddInt32(&f.concurrent, 1)
	for {
		prev := atomic.LoadInt32(&f.maxConcurrent)
		if now <= prev || atomic.CompareAndSwapInt32(&f.maxConcurrent, prev, now) {
			break
		}
	}
	defer atomic.AddInt32(&f.concurrent, -1)

	f.mu.Lock()
	defer f.mu.Unlock()
	if f.failTimes > 0 {
		f.failTimes--
		return f.failWith
	}
	f.publishes = append(f.publishes, publishCall{summary: summary, files: files})
	for _, file := range files {
		f.existing[file.Path] = file.Version()
	}
	return nil
}

func (f *fakeRemote) Download(_ context.Context, path string) ([]byte, error) {
	atomic.AddInt32(&f.downloads, 1)
	f.mu.Lock()
	defer f.mu.Unlock()
	if _, ok := f.existing[path]; !ok {
		return nil, &hf.APIError{Status: http.StatusNotFound, Message: "missing"}
	}
	return []byte("content of " + path), nil
}

func (f *fakeRemote) calls() []publishCall {
	f.mu.Lock()
	defer f.mu.Unlock()
	out := make([]publishCall, len(f.publishes))
	copy(out, f.publishes)
	return out
}

func quietLogger() *slog.Logger {
	return slog.New(slog.NewTextHandler(io.Discard, nil))
}

func testConfig() Config {
	c := DefaultConfig()
	c.Interval = 20 * time.Millisecond
	c.MinInterval = 10 * time.Millisecond
	c.MaxInterval = 80 * time.Millisecond
	c.MaxFiles = 3
	c.RetryBase = time.Millisecond
	c.MaxRetries = 2
	return c
}

func TestEnqueueCoalescesBatches(t *testing.T) {
	remote := newFake()
	c := New(testConfig(), remote, quietLogger())

	for i := 0; i < 7; i++ {
		c.Enqueue(fmt.Sprintf("f%d.json", i), []byte(fmt.Sprintf("v%d", i)))
	}
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatalf("flush: %v", err)
	}
	calls := remote.calls()
	// MaxFiles is 3, so 7 files must arrive as 3 commits.
	if len(calls) != 3 {
		t.Fatalf("expected 3 batched commits, got %d", len(calls))
	}
	total := 0
	for _, call := range calls {
		total += len(call.files)
		if len(call.files) > 3 {
			t.Fatalf("batch exceeded MaxFiles: %d", len(call.files))
		}
	}
	if total != 7 {
		t.Fatalf("expected 7 files published, got %d", total)
	}
}

func TestDuplicatePathKeepsNewest(t *testing.T) {
	remote := newFake()
	c := New(testConfig(), remote, quietLogger())

	for i := 0; i < 5; i++ {
		c.Enqueue("same.json", []byte(fmt.Sprintf("version-%d", i)))
	}
	if c.Queued() != 1 {
		t.Fatalf("queue should deduplicate to 1 path, got %d", c.Queued())
	}
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatal(err)
	}
	calls := remote.calls()
	if len(calls) != 1 || len(calls[0].files) != 1 {
		t.Fatalf("expected one commit with one file, got %+v", calls)
	}
	if string(calls[0].files[0].Content) != "version-4" {
		t.Fatalf("expected the newest version, got %q", calls[0].files[0].Content)
	}
}

func TestUnchangedFilesAreSkipped(t *testing.T) {
	remote := newFake()
	c := New(testConfig(), remote, quietLogger())

	c.Enqueue("a.json", []byte("hello"))
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatal(err)
	}
	// Same content again: no second commit should be made.
	c.Enqueue("a.json", []byte("hello"))
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatal(err)
	}
	if got := len(remote.calls()); got != 1 {
		t.Fatalf("unchanged content must not be republished, got %d commits", got)
	}
	if c.Stats().Skipped == 0 {
		t.Fatal("expected the skip counter to move")
	}

	// Changed content must publish.
	c.Enqueue("a.json", []byte("hello2"))
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatal(err)
	}
	if got := len(remote.calls()); got != 2 {
		t.Fatalf("changed content must publish, got %d commits", got)
	}
}

// slowRemote widens the race window so the serialisation guarantee is actually
// exercised rather than merely plausible. An earlier version of the controller
// passed a fast fake while still allowing six concurrent commits.
type slowRemote struct {
	*fakeRemote
	inFlight int32
	peak     int32
	calls    int32
}

func (s *slowRemote) Publish(ctx context.Context, summary string, files []hf.File) error {
	now := atomic.AddInt32(&s.inFlight, 1)
	defer atomic.AddInt32(&s.inFlight, -1)
	for {
		prev := atomic.LoadInt32(&s.peak)
		if now <= prev || atomic.CompareAndSwapInt32(&s.peak, prev, now) {
			break
		}
	}
	atomic.AddInt32(&s.calls, 1)
	time.Sleep(15 * time.Millisecond)
	return nil
}

func TestPublishesAreSerialised(t *testing.T) {
	remote := &slowRemote{fakeRemote: newFake()}
	cfg := testConfig()
	cfg.MaxFiles = 1
	c := New(cfg, remote, quietLogger())

	var wg sync.WaitGroup
	for w := 0; w < 6; w++ {
		wg.Add(1)
		go func(worker int) {
			defer wg.Done()
			for i := 0; i < 5; i++ {
				c.Enqueue(fmt.Sprintf("w%d-%d.json", worker, i), []byte(fmt.Sprint(i)))
				_ = c.FlushNow(context.Background())
			}
		}(w)
	}
	wg.Wait()

	if peak := atomic.LoadInt32(&remote.peak); peak > 1 {
		t.Fatalf("commits overlapped; peak concurrency = %d (serialisation is broken)", peak)
	}
	if atomic.LoadInt32(&remote.calls) == 0 {
		t.Fatal("no commit was attempted, so the test proved nothing")
	}
}

func TestRetryThenSuccess(t *testing.T) {
	remote := newFake()
	remote.failTimes = 2
	remote.failWith = &hf.APIError{Status: http.StatusTooManyRequests, Message: "rate limited"}

	c := New(testConfig(), remote, quietLogger())
	c.Enqueue("retry.json", []byte("x"))
	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatalf("expected eventual success, got %v", err)
	}
	if got := len(remote.calls()); got != 1 {
		t.Fatalf("expected one successful commit, got %d", got)
	}
	if c.Stats().Retries == 0 {
		t.Fatal("a retried publish should be counted as a retry")
	}
	if c.Stats().Commits != 1 {
		t.Fatalf("expected exactly one succeeded commit, got %d", c.Stats().Commits)
	}
}

func TestPermanentFailureRequeues(t *testing.T) {
	remote := newFake()
	remote.failTimes = 100
	remote.failWith = &hf.APIError{Status: http.StatusUnauthorized, Message: "bad token"}

	c := New(testConfig(), remote, quietLogger())
	c.Enqueue("perm.json", []byte("x"))
	if err := c.FlushNow(context.Background()); err == nil {
		t.Fatal("expected an error for an unauthorised publish")
	}
	if c.Queued() != 1 {
		t.Fatalf("failed batch must be requeued, queue=%d", c.Queued())
	}
	if opts := c.Stats().LastError; opts == "" {
		t.Fatal("last error should be recorded")
	}
}

func TestBackoffWidensThenNarrows(t *testing.T) {
	remote := newFake()
	// A non-retryable error means the batch fails outright, which must widen
	// the interval so a broken token does not hammer the API every tick.
	remote.failTimes = 1
	remote.failWith = &hf.APIError{Status: http.StatusForbidden, Message: "forbidden"}

	c := New(testConfig(), remote, quietLogger())
	base := c.currentInterval()

	c.Enqueue("b.json", []byte("x"))
	if err := c.FlushNow(context.Background()); err == nil {
		t.Fatal("expected the flush to fail")
	}
	widened := c.currentInterval()
	if widened <= base {
		t.Fatalf("interval should widen on failure: base=%v now=%v", base, widened)
	}

	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatal(err)
	}
	if c.currentInterval() >= widened {
		t.Fatal("interval should narrow after a success")
	}
}

func TestRateLimitRetryStillBacksOff(t *testing.T) {
	remote := newFake()
	remote.failTimes = 1
	remote.failWith = &hf.APIError{Status: http.StatusTooManyRequests, Message: "slow down"}

	c := New(testConfig(), remote, quietLogger())
	base := c.currentInterval()
	c.Enqueue("rl.json", []byte("x"))

	if err := c.FlushNow(context.Background()); err != nil {
		t.Fatalf("retry should have rescued the batch: %v", err)
	}
	if c.currentInterval() <= base {
		t.Fatal("being rate limited must widen the interval even when the retry succeeds")
	}
	if c.Stats().Retries == 0 {
		t.Fatal("the retry should be counted")
	}
}

func TestQueueBudgetDropsInsteadOfGrowing(t *testing.T) {
	remote := newFake()
	cfg := testConfig()
	cfg.MaxQueued = 10
	c := New(cfg, remote, quietLogger())

	for i := 0; i < 100; i++ {
		c.Enqueue(fmt.Sprintf("f%d", i), []byte("x"))
	}
	if c.Queued() > 10 {
		t.Fatalf("queue must stay bounded, got %d", c.Queued())
	}
	if c.Stats().Dropped == 0 {
		t.Fatal("drops must be counted")
	}
}

func TestRunLoopFlushesOnShutdown(t *testing.T) {
	remote := newFake()
	c := New(testConfig(), remote, quietLogger())
	c.Enqueue("shutdown.json", []byte("value"))

	ctx, cancel := context.WithCancel(context.Background())
	finished := make(chan struct{})
	go func() { c.Run(ctx); close(finished) }()

	cancel() // behaves like SIGTERM: the final flush must still happen
	select {
	case <-finished:
	case <-time.After(2 * time.Second):
		t.Fatal("Run did not exit")
	}
	if got := len(remote.calls()); got != 1 {
		t.Fatalf("queued work must be flushed on shutdown, got %d commits", got)
	}
}

func TestRunLoopPublishesPeriodically(t *testing.T) {
	remote := newFake()
	c := New(testConfig(), remote, quietLogger())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	go c.Run(ctx)

	c.Enqueue("periodic.json", []byte("v"))
	deadline := time.After(2 * time.Second)
	for {
		if len(remote.calls()) == 1 {
			return
		}
		select {
		case <-deadline:
			t.Fatal("periodic flush never happened")
		case <-time.After(5 * time.Millisecond):
		}
	}
}

func TestRestoreDownloadsInParallel(t *testing.T) {
	remote := newFake()
	remote.existing = map[string]string{
		"identities/ab/one.json": hf.BlobSHA([]byte("1")),
		"identities/ab/two.json": hf.BlobSHA([]byte("2")),
		"events/day.jsonl":       hf.BlobSHA([]byte("3")),
	}
	dir := t.TempDir()
	n, err := Restore(context.Background(), remote, "", dir, 4)
	if err != nil {
		t.Fatalf("restore: %v", err)
	}
	if n != 3 {
		t.Fatalf("expected 3 files restored, got %d", n)
	}
	if atomic.LoadInt32(&remote.downloads) != 3 {
		t.Fatalf("expected 3 downloads, got %d", remote.downloads)
	}
}

func TestBlobSHAIsStable(t *testing.T) {
	a := hf.BlobSHA([]byte("hello"))
	b := hf.BlobSHA([]byte("hello"))
	c := hf.BlobSHA([]byte("hello!"))
	if a != b {
		t.Fatal("blob hash must be deterministic")
	}
	if a == c {
		t.Fatal("different content must hash differently")
	}
	if len(a) != 40 {
		t.Fatalf("expected a 40-char git sha, got %d", len(a))
	}
}

func TestSanitizePath(t *testing.T) {
	cases := map[string]string{
		"/identities/": "identities",
		"identities//": "identities",
		"  raw  ":      "raw",
	}
	for in, want := range cases {
		if got := SanitizePath(in); got != want {
			t.Fatalf("SanitizePath(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestRetryableClassification(t *testing.T) {
	retryable := []int{429, 500, 502, 503, 504}
	for _, status := range retryable {
		err := &hf.APIError{Status: status}
		if !isRetryable(err) {
			t.Fatalf("status %d should be retryable", status)
		}
	}
	permanent := []int{400, 401, 403, 404, 422}
	for _, status := range permanent {
		err := &hf.APIError{Status: status}
		if isRetryable(err) {
			t.Fatalf("status %d should not be retried", status)
		}
	}
	if isRetryable(context.Canceled) {
		t.Fatal("cancellation must not be retried")
	}
	if !isRetryable(errors.New("connection reset")) {
		t.Fatal("transport errors should be retried")
	}
}

// Regression: MaxInterval used to be left at zero, so the first failed flush
// clamped the interval to 0 and the controller retried in a tight loop against
// a dataset that was already failing. That is the worst possible time to
// multiply the request rate.
func TestBackoffNeverCollapsesToZero(t *testing.T) {
	// Deliberately omit MinInterval/MaxInterval, as main.go does.
	c := New(Config{Interval: 30 * time.Second, MaxFiles: 10}, &fakeRemote{}, nil)

	c.widen()
	if got := c.currentInterval(); got <= 0 {
		t.Fatalf("interval after one backoff = %v, want > 0", got)
	}
	prev := c.currentInterval()
	for i := 0; i < 20; i++ {
		c.widen()
		cur := c.currentInterval()
		if cur < prev {
			t.Fatalf("interval shrank on backoff: %v -> %v", prev, cur)
		}
		if cur > 10*time.Minute {
			t.Fatalf("interval %v exceeded the 10m ceiling", cur)
		}
		prev = cur
	}
	if prev != 10*time.Minute {
		t.Fatalf("after 20 backoffs interval = %v, want the 10m ceiling", prev)
	}

	// And narrow() must not undershoot the floor.
	for i := 0; i < 60; i++ {
		c.narrow()
	}
	if got := c.currentInterval(); got < 15*time.Second {
		t.Fatalf("interval after repeated narrowing = %v, want >= 15s", got)
	}
}