{"version":3,"file":"realtimeStt.test.mjs","names":[],"sources":["../src/realtimeStt.test.ts"],"sourcesContent":["import { describe, it } from \"@effect/vitest\"\nimport { Effect, Exit, Fiber, Redacted, Stream } from \"effect\"\nimport { expect } from \"vitest\"\nimport * as FakeWebSocket from \"@effect-uai/core/testing/FakeWebSocket\"\nimport { frameToEvent, streamTranscription } from \"./realtimeStt.js\"\n\nconst decode = (frame: unknown) => frameToEvent(JSON.stringify(frame))\n\ndescribe(\"OpenAI Realtime STT frame mapper\", () => {\n  it.effect(\"decodes GA transcript frames and converts VAD offsets from ms to seconds\", () =>\n    Effect.gen(function* () {\n      expect(\n        yield* decode({\n          type: \"conversation.item.input_audio_transcription.delta\",\n          item_id: \"item_1\",\n          content_index: 0,\n          delta: \"hel\",\n        }),\n      ).toEqual({ _tag: \"partial\", text: \"hel\" })\n      expect(\n        yield* decode({\n          type: \"conversation.item.input_audio_transcription.completed\",\n          item_id: \"item_1\",\n          content_index: 0,\n          transcript: \"hello\",\n          usage: { type: \"duration\", seconds: 1.2 },\n        }),\n      ).toEqual({ _tag: \"final\", text: \"hello\" })\n      expect(\n        yield* decode({ type: \"input_audio_buffer.speech_started\", audio_start_ms: 1500 }),\n      ).toEqual({ _tag: \"speech-started\", atSeconds: 1.5 })\n      expect(\n        yield* decode({ type: \"input_audio_buffer.speech_stopped\", audio_end_ms: 3200 }),\n      ).toEqual({ _tag: \"utterance-ended\", atSeconds: 3.2 })\n    }),\n  )\n\n  it.effect(\"elides a null error code instead of forwarding it\", () =>\n    Effect.gen(function* () {\n      expect(\n        yield* decode({\n          type: \"error\",\n          error: { type: \"invalid_request_error\", code: null, message: \"bad\" },\n        }),\n      ).toEqual({ _tag: \"error\", message: \"bad\" })\n    }),\n  )\n\n  it.effect(\"drops handshake acks, unknown frames and malformed text\", () =>\n    Effect.gen(function* () {\n      expect(yield* decode({ type: \"session.updated\", session: { type: \"transcription\" } })).toBe(\n        undefined,\n      )\n      expect(yield* decode({ type: \"input_audio_buffer.committed\", item_id: \"i\" })).toBe(undefined)\n      expect(yield* frameToEvent(\"not json\")).toBe(undefined)\n    }),\n  )\n})\n\nconst pcm = { container: \"raw\", encoding: \"pcm_s16le\", sampleRate: 24000, channels: 1 } as const\n\nconst request = { model: \"gpt-4o-transcribe\", inputFormat: pcm }\n\nconst transcribing = FakeWebSocket.make({\n  greeting: [\n    { type: \"conversation.item.input_audio_transcription.delta\", delta: \"hel\" },\n    { type: \"conversation.item.input_audio_transcription.completed\", transcript: \"hello\" },\n  ],\n})\n\n/** Run a transcription against `server`, closing it, if asked, once the frames are in. */\nconst transcribe = <E>(\n  server: FakeWebSocket.FakeWebSocketServer,\n  audioIn: Stream.Stream<Uint8Array, E>,\n  close?: { readonly code: number; readonly afterSent?: number },\n) =>\n  Effect.gen(function* () {\n    const run = streamTranscription({\n      apiKey: Redacted.make(\"test-key\"),\n      webSocket: server.connect,\n    })\n    const collector = yield* Effect.forkChild(Effect.exit(Stream.runCollect(run(audioIn, request))))\n    if (close !== undefined) {\n      yield* server.greeted\n      yield* server.awaitSent(close.afterSent ?? 0)\n      yield* server.close(close.code)\n    }\n    return yield* Fiber.join(collector)\n  })\n\ndescribe(\"OpenAI Realtime STT socket\", () => {\n  it.live(\"configures the session before any audio, and ends on a clean close\", () =>\n    Effect.gen(function* () {\n      const server = yield* transcribing\n      const exit = yield* transcribe(server, Stream.make(new Uint8Array([1, 2])), {\n        code: 1000,\n        afterSent: 2,\n      })\n\n      expect(exit).toEqual(\n        Exit.succeed([\n          { _tag: \"partial\", text: \"hel\" },\n          { _tag: \"final\", text: \"hello\" },\n        ]),\n      )\n      const sent = (yield* server.sent) as ReadonlyArray<{ type: string }>\n      expect(sent.map((f) => f.type)).toEqual([\"session.update\", \"input_audio_buffer.append\"])\n    }).pipe(Effect.scoped),\n  )\n\n  it.live(\"fails rather than ending when the connection drops\", () =>\n    Effect.gen(function* () {\n      const server = yield* transcribing\n      const exit = yield* transcribe(server, Stream.never, { code: 1011 })\n\n      expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain(\"Unavailable\")\n    }).pipe(Effect.scoped),\n  )\n\n  it.live(\"fails with the audio input's own error\", () =>\n    Effect.gen(function* () {\n      const server = yield* transcribing\n      const exit = yield* transcribe(server, Stream.fail(\"microphone unplugged\"))\n\n      expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain(\"microphone unplugged\")\n    }).pipe(Effect.scoped),\n  )\n})\n"],"mappings":";;;;;;AAMA,MAAM,UAAU,UAAmB,aAAa,KAAK,UAAU,KAAK,CAAC;AAErE,SAAS,0CAA0C;CACjD,GAAG,OAAO,kFACR,OAAO,IAAI,aAAa;EACtB,aACE,OAAO,OAAO;GACZ,MAAM;GACN,SAAS;GACT,eAAe;GACf,OAAO;EACT,CAAC,CACH,CAAC,CAAC,QAAQ;GAAE,MAAM;GAAW,MAAM;EAAM,CAAC;EAC1C,aACE,OAAO,OAAO;GACZ,MAAM;GACN,SAAS;GACT,eAAe;GACf,YAAY;GACZ,OAAO;IAAE,MAAM;IAAY,SAAS;GAAI;EAC1C,CAAC,CACH,CAAC,CAAC,QAAQ;GAAE,MAAM;GAAS,MAAM;EAAQ,CAAC;EAC1C,aACE,OAAO,OAAO;GAAE,MAAM;GAAqC,gBAAgB;EAAK,CAAC,CACnF,CAAC,CAAC,QAAQ;GAAE,MAAM;GAAkB,WAAW;EAAI,CAAC;EACpD,aACE,OAAO,OAAO;GAAE,MAAM;GAAqC,cAAc;EAAK,CAAC,CACjF,CAAC,CAAC,QAAQ;GAAE,MAAM;GAAmB,WAAW;EAAI,CAAC;CACvD,CAAC,CACH;CAEA,GAAG,OAAO,2DACR,OAAO,IAAI,aAAa;EACtB,aACE,OAAO,OAAO;GACZ,MAAM;GACN,OAAO;IAAE,MAAM;IAAyB,MAAM;IAAM,SAAS;GAAM;EACrE,CAAC,CACH,CAAC,CAAC,QAAQ;GAAE,MAAM;GAAS,SAAS;EAAM,CAAC;CAC7C,CAAC,CACH;CAEA,GAAG,OAA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