File size: 1,971 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from 'vitest';
import { estimateTTSTime } from '@/utils/ttsTime';
import { BookProgress } from '@/types/book';

const createProgress = (values: {
  sectionMin: number;
  totalMin: number;
  pageCurrent: number;
  pageTotal: number;
}) => {
  return {
    timeinfo: {
      section: values.sectionMin,
      total: values.totalMin,
    },
    pageinfo: {
      current: values.pageCurrent,
      total: values.pageTotal,
    },
  } as BookProgress;
};

describe('estimateTTSTime', () => {
  it('estimates chapter and book remaining time at 1x', () => {
    const progress = createProgress({
      sectionMin: 10,
      totalMin: 60,
      pageCurrent: 49,
      pageTotal: 100,
    });

    const result = estimateTTSTime(progress, 1, 1_700_000_000_000);

    expect(result.chapterRemainingSec).toBe(600);
    expect(result.bookRemainingSec).toBe(3600);
    expect(result.finishAtTimestamp).toBe(1_700_003_600_000);
  });

  it('scales estimates by playback rate', () => {
    const progress = createProgress({
      sectionMin: 12,
      totalMin: 90,
      pageCurrent: 44,
      pageTotal: 90,
    });

    const result = estimateTTSTime(progress, 1.5, 1000);

    expect(result.chapterRemainingSec).toBe(480);
    expect(result.bookRemainingSec).toBe(3600);
    expect(result.finishAtTimestamp).toBe(3_601_000);
  });

  it('falls back safely for missing progress', () => {
    const result = estimateTTSTime(null, 2, 1000);

    expect(result.chapterRemainingSec).toBeNull();
    expect(result.bookRemainingSec).toBeNull();
    expect(result.finishAtTimestamp).toBeNull();
  });

  it('uses book remaining to compute finish time', () => {
    const progress = createProgress({
      sectionMin: 1,
      totalMin: 2,
      pageCurrent: 99,
      pageTotal: 100,
    });

    const result = estimateTTSTime(progress, 1, 1000);

    expect(result.bookRemainingSec).toBe(120);
    expect(result.finishAtTimestamp).toBe(121000);
  });
});