aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos/video-captions.ts
blob: 3bb0d131cba6db5d9a8d01e990a97a7701f3e6b0 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import {
  checkVideoFilesWereRemoved,
  cleanupTests,
  createMultipleServers,
  doubleFollow,
  PeerTubeServer,
  setAccessTokensToServers,
  testCaptionFile,
  wait,
  waitJobs
} from '@shared/extra-utils'

const expect = chai.expect

describe('Test video captions', function () {
  const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'

  let servers: PeerTubeServer[]
  let videoUUID: string

  before(async function () {
    this.timeout(60000)

    servers = await createMultipleServers(2)

    await setAccessTokensToServers(servers)
    await doubleFollow(servers[0], servers[1])

    await waitJobs(servers)

    const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
    videoUUID = uuid

    await waitJobs(servers)
  })

  it('Should list the captions and return an empty list', async function () {
    for (const server of servers) {
      const body = await server.captions.list({ videoId: videoUUID })
      expect(body.total).to.equal(0)
      expect(body.data).to.have.lengthOf(0)
    }
  })

  it('Should create two new captions', async function () {
    this.timeout(30000)

    await servers[0].captions.add({
      language: 'ar',
      videoId: videoUUID,
      fixture: 'subtitle-good1.vtt'
    })

    await servers[0].captions.add({
      language: 'zh',
      videoId: videoUUID,
      fixture: 'subtitle-good2.vtt',
      mimeType: 'application/octet-stream'
    })

    await waitJobs(servers)
  })

  it('Should list these uploaded captions', async function () {
    for (const server of servers) {
      const body = await server.captions.list({ videoId: videoUUID })
      expect(body.total).to.equal(2)
      expect(body.data).to.have.lengthOf(2)

      const caption1 = body.data[0]
      expect(caption1.language.id).to.equal('ar')
      expect(caption1.language.label).to.equal('Arabic')
      expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
      await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')

      const caption2 = body.data[1]
      expect(caption2.language.id).to.equal('zh')
      expect(caption2.language.label).to.equal('Chinese')
      expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
      await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
    }
  })

  it('Should replace an existing caption', async function () {
    this.timeout(30000)

    await servers[0].captions.add({
      language: 'ar',
      videoId: videoUUID,
      fixture: 'subtitle-good2.vtt'
    })

    await waitJobs(servers)
  })

  it('Should have this caption updated', async function () {
    for (const server of servers) {
      const body = await server.captions.list({ videoId: videoUUID })
      expect(body.total).to.equal(2)
      expect(body.data).to.have.lengthOf(2)

      const caption1 = body.data[0]
      expect(caption1.language.id).to.equal('ar')
      expect(caption1.language.label).to.equal('Arabic')
      expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
      await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
    }
  })

  it('Should replace an existing caption with a srt file and convert it', async function () {
    this.timeout(30000)

    await servers[0].captions.add({
      language: 'ar',
      videoId: videoUUID,
      fixture: 'subtitle-good.srt'
    })

    await waitJobs(servers)

    // Cache invalidation
    await wait(3000)
  })

  it('Should have this caption updated and converted', async function () {
    for (const server of servers) {
      const body = await server.captions.list({ videoId: videoUUID })
      expect(body.total).to.equal(2)
      expect(body.data).to.have.lengthOf(2)

      const caption1 = body.data[0]
      expect(caption1.language.id).to.equal('ar')
      expect(caption1.language.label).to.equal('Arabic')
      expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))

      const expected = 'WEBVTT FILE\r\n' +
        '\r\n' +
        '1\r\n' +
        '00:00:01.600 --> 00:00:04.200\r\n' +
        'English (US)\r\n' +
        '\r\n' +
        '2\r\n' +
        '00:00:05.900 --> 00:00:07.999\r\n' +
        'This is a subtitle in American English\r\n' +
        '\r\n' +
        '3\r\n' +
        '00:00:10.000 --> 00:00:14.000\r\n' +
        'Adding subtitles is very easy to do\r\n'
      await testCaptionFile(server.url, caption1.captionPath, expected)
    }
  })

  it('Should remove one caption', async function () {
    this.timeout(30000)

    await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })

    await waitJobs(servers)
  })

  it('Should only list the caption that was not deleted', async function () {
    for (const server of servers) {
      const body = await server.captions.list({ videoId: videoUUID })
      expect(body.total).to.equal(1)
      expect(body.data).to.have.lengthOf(1)

      const caption = body.data[0]

      expect(caption.language.id).to.equal('zh')
      expect(caption.language.label).to.equal('Chinese')
      expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
      await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
    }
  })

  it('Should remove the video, and thus all video captions', async function () {
    const video = await servers[0].videos.get({ id: videoUUID })
    const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })

    await servers[0].videos.remove({ id: videoUUID })

    await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})