videos-history.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { wait } from '@shared/core-utils'
  4. import { Video } from '@shared/models'
  5. import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
  6. describe('Test videos history', function () {
  7. let server: PeerTubeServer = null
  8. let video1Id: number
  9. let video1UUID: string
  10. let video2UUID: string
  11. let video3UUID: string
  12. let video3WatchedDate: Date
  13. let userAccessToken: string
  14. before(async function () {
  15. this.timeout(120000)
  16. server = await createSingleServer(1)
  17. await setAccessTokensToServers([ server ])
  18. // 10 seconds long
  19. const fixture = 'video_short1.webm'
  20. {
  21. const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
  22. video1UUID = uuid
  23. video1Id = id
  24. }
  25. {
  26. const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
  27. video2UUID = uuid
  28. }
  29. {
  30. const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
  31. video3UUID = uuid
  32. }
  33. userAccessToken = await server.users.generateUserAndToken('user_1')
  34. })
  35. it('Should get videos, without watching history', async function () {
  36. const { data } = await server.videos.listWithToken()
  37. for (const video of data) {
  38. const videoDetails = await server.videos.getWithToken({ id: video.id })
  39. expect(video.userHistory).to.be.undefined
  40. expect(videoDetails.userHistory).to.be.undefined
  41. }
  42. })
  43. it('Should watch the first and second video', async function () {
  44. await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
  45. await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
  46. })
  47. it('Should return the correct history when listing, searching and getting videos', async function () {
  48. const videosOfVideos: Video[][] = []
  49. {
  50. const { data } = await server.videos.listWithToken()
  51. videosOfVideos.push(data)
  52. }
  53. {
  54. const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
  55. videosOfVideos.push(body.data)
  56. }
  57. for (const videos of videosOfVideos) {
  58. const video1 = videos.find(v => v.uuid === video1UUID)
  59. const video2 = videos.find(v => v.uuid === video2UUID)
  60. const video3 = videos.find(v => v.uuid === video3UUID)
  61. expect(video1.userHistory).to.not.be.undefined
  62. expect(video1.userHistory.currentTime).to.equal(3)
  63. expect(video2.userHistory).to.not.be.undefined
  64. expect(video2.userHistory.currentTime).to.equal(8)
  65. expect(video3.userHistory).to.be.undefined
  66. }
  67. {
  68. const videoDetails = await server.videos.getWithToken({ id: video1UUID })
  69. expect(videoDetails.userHistory).to.not.be.undefined
  70. expect(videoDetails.userHistory.currentTime).to.equal(3)
  71. }
  72. {
  73. const videoDetails = await server.videos.getWithToken({ id: video2UUID })
  74. expect(videoDetails.userHistory).to.not.be.undefined
  75. expect(videoDetails.userHistory.currentTime).to.equal(8)
  76. }
  77. {
  78. const videoDetails = await server.videos.getWithToken({ id: video3UUID })
  79. expect(videoDetails.userHistory).to.be.undefined
  80. }
  81. })
  82. it('Should have these videos when listing my history', async function () {
  83. video3WatchedDate = new Date()
  84. await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
  85. const body = await server.history.list()
  86. expect(body.total).to.equal(3)
  87. const videos = body.data
  88. expect(videos[0].name).to.equal('video 3')
  89. expect(videos[1].name).to.equal('video 1')
  90. expect(videos[2].name).to.equal('video 2')
  91. })
  92. it('Should not have videos history on another user', async function () {
  93. const body = await server.history.list({ token: userAccessToken })
  94. expect(body.total).to.equal(0)
  95. expect(body.data).to.have.lengthOf(0)
  96. })
  97. it('Should be able to search through videos in my history', async function () {
  98. const body = await server.history.list({ search: '2' })
  99. expect(body.total).to.equal(1)
  100. const videos = body.data
  101. expect(videos[0].name).to.equal('video 2')
  102. })
  103. it('Should clear my history', async function () {
  104. await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
  105. })
  106. it('Should have my history cleared', async function () {
  107. const body = await server.history.list()
  108. expect(body.total).to.equal(1)
  109. const videos = body.data
  110. expect(videos[0].name).to.equal('video 3')
  111. })
  112. it('Should disable videos history', async function () {
  113. await server.users.updateMe({
  114. videosHistoryEnabled: false
  115. })
  116. await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
  117. const { data } = await server.history.list()
  118. expect(data[0].name).to.not.equal('video 2')
  119. })
  120. it('Should re-enable videos history', async function () {
  121. await server.users.updateMe({
  122. videosHistoryEnabled: true
  123. })
  124. await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
  125. const { data } = await server.history.list()
  126. expect(data[0].name).to.equal('video 2')
  127. })
  128. it('Should not clean old history', async function () {
  129. this.timeout(50000)
  130. await killallServers([ server ])
  131. await server.run({ history: { videos: { max_age: '10 days' } } })
  132. await wait(6000)
  133. // Should still have history
  134. const body = await server.history.list()
  135. expect(body.total).to.equal(2)
  136. })
  137. it('Should clean old history', async function () {
  138. this.timeout(50000)
  139. await killallServers([ server ])
  140. await server.run({ history: { videos: { max_age: '5 seconds' } } })
  141. await wait(6000)
  142. const body = await server.history.list()
  143. expect(body.total).to.equal(0)
  144. })
  145. it('Should delete a specific history element', async function () {
  146. {
  147. await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
  148. await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
  149. }
  150. {
  151. const body = await server.history.list()
  152. expect(body.total).to.equal(2)
  153. }
  154. {
  155. await server.history.removeElement({ videoId: video1Id })
  156. const body = await server.history.list()
  157. expect(body.total).to.equal(1)
  158. expect(body.data[0].uuid).to.equal(video2UUID)
  159. }
  160. })
  161. after(async function () {
  162. await cleanupTests([ server ])
  163. })
  164. })