grok_audio_billing_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //go:build unit
  2. package handler
  3. import (
  4. "testing"
  5. "time"
  6. coderws "github.com/coder/websocket"
  7. )
  8. func TestIsExpectedGrokRealtimeClose(t *testing.T) {
  9. for _, status := range []coderws.StatusCode{
  10. coderws.StatusNormalClosure,
  11. coderws.StatusGoingAway,
  12. coderws.StatusNoStatusRcvd,
  13. coderws.StatusAbnormalClosure,
  14. } {
  15. if !isExpectedGrokRealtimeClose(coderws.CloseError{Code: status}) {
  16. t.Fatalf("status %v should be treated as an expected session close", status)
  17. }
  18. }
  19. if isExpectedGrokRealtimeClose(coderws.CloseError{Code: coderws.StatusPolicyViolation}) {
  20. t.Fatal("policy violations must not be treated as billable normal closes")
  21. }
  22. }
  23. func TestGrokRealtimeBillingResultRequiresObservedAudio(t *testing.T) {
  24. if grokRealtimeBillingResult("grok-voice-latest", time.Second, false) != nil {
  25. t.Fatal("a session without observed audio must not be billed")
  26. }
  27. if grokRealtimeBillingResult("grok-voice-latest", 0, true) != nil {
  28. t.Fatal("zero-duration sessions must not be billed")
  29. }
  30. }
  31. func TestGrokRealtimeBillingResultUsesForcedUniqueID(t *testing.T) {
  32. first := grokRealtimeBillingResult("grok-voice-latest", 90*time.Second, true)
  33. second := grokRealtimeBillingResult("grok-voice-latest", 90*time.Second, true)
  34. if first == nil || second == nil {
  35. t.Fatal("observed audio sessions should be billable")
  36. }
  37. if first.RequestID == "" {
  38. t.Fatalf("unexpected billing request ID %q", first.RequestID)
  39. }
  40. if first.RequestID == second.RequestID {
  41. t.Fatal("independent realtime connections must not share a billing request ID")
  42. }
  43. if first.AudioUsage == nil || first.AudioUsage.Mode != "realtime" || first.AudioUsage.DurationOrUnits != 1.5 {
  44. t.Fatalf("unexpected audio usage: %#v", first.AudioUsage)
  45. }
  46. }