File size: 2,557 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  buildCloudV2TranscriptMetric,
  logCloudV2TranscriptMetric,
} from "../../../modules/engine/src/services/CloudTranscriptE2EMetrics"
import type {TranscriptionData} from "@mentra/cloud-runtime/protocol"

const transcript: TranscriptionData = {
  userId: "user-1",
  subscription: {kind: "transcription", language: {mode: "specific", code: "en-US"}},
  text: "hello world",
  isFinal: true,
  utteranceId: "utt-1",
  speakerId: "speaker-1",
  startMs: 100,
  endMs: 900,
  durationMs: 800,
  confidence: 0.9,
  resolvedLanguage: "en-US",
  languageDetected: false,
  tokens: [
    {
      text: "hello",
      startMs: 100,
      endMs: 400,
      confidence: 0.9,
      isFinal: true,
    },
  ],
  provider: "soniox",
  timestamp: 123456,
}

describe("CloudTranscriptE2EMetrics", () => {
  const originalEnv = process.env.EXPO_PUBLIC_ENABLE_E2E_METRICS
  let nowSpy: jest.SpyInstance
  let logSpy: jest.SpyInstance

  beforeEach(() => {
    nowSpy = jest.spyOn(Date, "now").mockReturnValue(777)
    logSpy = jest.spyOn(console, "log").mockImplementation(() => {})
  })

  afterEach(() => {
    if (originalEnv === undefined) {
      delete process.env.EXPO_PUBLIC_ENABLE_E2E_METRICS
    } else {
      process.env.EXPO_PUBLIC_ENABLE_E2E_METRICS = originalEnv
    }
    jest.restoreAllMocks()
  })

  it("builds the cloud v2 transcript metric payload", () => {
    expect(buildCloudV2TranscriptMetric(transcript)).toMatchObject({
      event: "cloud_v2_transcript",
      ts_ms: 777,
      text: "hello world",
      state: "final",
      is_final: true,
      resolved_language: "en-US",
      language_detected: false,
      utterance_id: "utt-1",
      start_ms: 100,
      end_ms: 900,
      duration_ms: 800,
      provider: "soniox",
      timestamp_ms: 123456,
      token_count: 1,
    })
    expect(nowSpy).toHaveBeenCalled()
  })

  it("logs only when e2e metrics are enabled", () => {
    process.env.EXPO_PUBLIC_ENABLE_E2E_METRICS = "false"
    logCloudV2TranscriptMetric(transcript)
    expect(logSpy).not.toHaveBeenCalled()

    process.env.EXPO_PUBLIC_ENABLE_E2E_METRICS = "true"
    logCloudV2TranscriptMetric(transcript)

    expect(logSpy).toHaveBeenCalledTimes(1)
    const line = String(logSpy.mock.calls[0][0])
    expect(line.startsWith("E2E_METRIC ")).toBe(true)
    expect(JSON.parse(line.replace("E2E_METRIC ", ""))).toMatchObject({
      event: "cloud_v2_transcript",
      text: "hello world",
      is_final: true,
    })
  })
})