arblant
firefox-vpn-client / core / internal / proxy / status_test.go
status_test.go70 lines · 2.1 KB
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
package proxy

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

	"github.com/armanlife/firefox-vpn-client/core/internal/guardian"
)

func TestProxyControllerWritesStatusFile(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	statusFile := filepath.Join(dir, "status.json")
	controller := &Controller{
		statusFile: statusFile,
		current: &managedSession{
			session:   &fakeProxySession{},
			expiresAt: time.Now().Add(10 * time.Minute),
			accepting: true,
		},
	}

	controller.writeStatus("startup")
	controller.writeStatus("renewed")

	data, err := os.ReadFile(statusFile)
	if err != nil {
		t.Fatalf("ReadFile returned error: %v", err)
	}
	var status RuntimeStatus
	if err := json.Unmarshal(data, &status); err != nil {
		t.Fatalf("json.Unmarshal returned error: %v", err)
	}
	if status.Reason != "renewed" {
		t.Fatalf("expected reason renewed, got %q", status.Reason)
	}
	if status.ProxyPassExpiresAt == "" {
		t.Fatal("expected proxy_pass_expires_at to be populated")
	}
	if !status.Accepting {
		t.Fatal("expected accepting=true")
	}
}

func TestProxyControllerStatusReportsQuotaAndFailure(t *testing.T) {
	t.Parallel()

	controller := &Controller{
		current: &managedSession{expiresAt: time.Now().Add(time.Minute), accepting: true},
		usage:   &guardian.ProxyUsage{Limit: 100, Remaining: 40, Reset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)},
	}
	controller.recordRenewalOutcome(fmt.Errorf("x: %w", guardian.ErrRegionUnavailable), time.Now().Add(time.Minute))
	status := controller.CurrentStatus("renewal_failed")
	if status.LastError != "region_unavailable" || status.NextAttemptAt == "" {
		t.Fatalf("expected failure fields, got %#v", status)
	}
	if status.QuotaRemainingBytes == nil || *status.QuotaRemainingBytes != 40 || status.QuotaResetAt != "2026-11-01T00:00:00Z" {
		t.Fatalf("expected quota fields, got %#v", status)
	}

	controller.recordRenewalOutcome(nil, time.Time{})
	if status := controller.CurrentStatus("token_updated"); status.LastError != "" || status.NextAttemptAt != "" {
		t.Fatalf("expected failure fields to clear after success, got %#v", status)
	}
}