"use strict";
var import_vitest = require("vitest");
var import_playback_payload = require("./playback_payload.cjs");
(0, import_vitest.describe)("parsePlaybackFinishedPayload", () => {
  (0, import_vitest.it)("parses the protocol-canonical snake_case payload (as emitted by Anam)", () => {
    const payload = '{"playback_position": 2.000000000000001, "interrupted": true, "synchronized_transcript": null}';
    const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)(payload);
    (0, import_vitest.expect)(ev.playbackPosition).toBeCloseTo(2);
    (0, import_vitest.expect)(ev.interrupted).toBe(true);
    (0, import_vitest.expect)(ev.synchronizedTranscript).toBeUndefined();
    const audioEndMs = Math.floor(ev.playbackPosition * 1e3);
    (0, import_vitest.expect)(Number.isFinite(audioEndMs)).toBe(true);
    (0, import_vitest.expect)(JSON.stringify({ audio_end_ms: audioEndMs })).toBe('{"audio_end_ms":2000}');
  });
  (0, import_vitest.it)("preserves a snake_case synchronized_transcript string", () => {
    const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)(
      '{"playback_position": 1.5, "interrupted": false, "synchronized_transcript": "hello there"}'
    );
    (0, import_vitest.expect)(ev.playbackPosition).toBe(1.5);
    (0, import_vitest.expect)(ev.interrupted).toBe(false);
    (0, import_vitest.expect)(ev.synchronizedTranscript).toBe("hello there");
  });
  (0, import_vitest.it)("tolerates camelCase keys as a fallback for JS-side producers", () => {
    const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)(
      '{"playbackPosition": 3.25, "interrupted": true, "synchronizedTranscript": "hi"}'
    );
    (0, import_vitest.expect)(ev.playbackPosition).toBe(3.25);
    (0, import_vitest.expect)(ev.interrupted).toBe(true);
    (0, import_vitest.expect)(ev.synchronizedTranscript).toBe("hi");
  });
  (0, import_vitest.it)("coerces a missing playback position to 0 (never NaN)", () => {
    const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)('{"interrupted": true}');
    (0, import_vitest.expect)(ev.playbackPosition).toBe(0);
    (0, import_vitest.expect)(ev.interrupted).toBe(true);
    (0, import_vitest.expect)(ev.synchronizedTranscript).toBeUndefined();
    (0, import_vitest.expect)(Number.isFinite(Math.floor(ev.playbackPosition * 1e3))).toBe(true);
  });
  (0, import_vitest.it)("coerces a null / non-numeric playback position to 0", () => {
    (0, import_vitest.expect)((0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": null}').playbackPosition).toBe(0);
    (0, import_vitest.expect)((0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": "1.5"}').playbackPosition).toBe(0);
  });
  (0, import_vitest.it)("keeps a legitimate zero position as 0", () => {
    const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": 0, "interrupted": true}');
    (0, import_vitest.expect)(ev.playbackPosition).toBe(0);
  });
  (0, import_vitest.it)("defaults interrupted to false when absent", () => {
    (0, import_vitest.expect)((0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": 1}').interrupted).toBe(false);
  });
  (0, import_vitest.it)("returns a safe default for a malformed / non-object payload (never throws or hangs)", () => {
    for (const payload of ["null", "", "123", '"a string"', "[]", "not json{"]) {
      const ev = (0, import_playback_payload.parsePlaybackFinishedPayload)(payload);
      (0, import_vitest.expect)(ev.playbackPosition).toBe(0);
      (0, import_vitest.expect)(ev.interrupted).toBe(false);
      (0, import_vitest.expect)(ev.synchronizedTranscript).toBeUndefined();
    }
  });
  (0, import_vitest.it)('only treats a real boolean interrupted as authoritative (not a truthy "false" string)', () => {
    (0, import_vitest.expect)(
      (0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": 2, "interrupted": "false"}').interrupted
    ).toBe(false);
    (0, import_vitest.expect)(
      (0, import_playback_payload.parsePlaybackFinishedPayload)('{"playback_position": 2, "interrupted": true}').interrupted
    ).toBe(true);
  });
});
//# sourceMappingURL=playback_payload.test.cjs.map