aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/src/files_views/columns.spec.ts
blob: 12fb1628bb3d6e30789c81726681549f575e3f83 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { File } from '@nextcloud/files'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { deleted, deletedBy, originalLocation } from './columns.ts'
import { trashbinView } from './trashbinView.ts'
import * as ncAuth from '@nextcloud/auth'

describe('files_trashbin: file list columns', () => {

	describe('column: original location', () => {
		it('has id set', () => {
			expect(originalLocation.id).toBe('files_trashbin--original-location')
		})

		it('has title set', () => {
			expect(originalLocation.title).toBe('Original location')
		})

		it('correctly sorts nodes by original location', () => {
			const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-original-location': 'z-folder/a.txt' } })
			const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', attributes: { 'trashbin-original-location': 'folder/b.txt' } })

			expect(originalLocation.sort).toBeTypeOf('function')
			expect(originalLocation.sort!(nodeA, nodeB)).toBeGreaterThan(0)
			expect(originalLocation.sort!(nodeB, nodeA)).toBeLessThan(0)
		})

		it('renders a node with original location', () => {
			const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-original-location': 'folder/a.txt' } })
			const el: HTMLElement = originalLocation.render(node, trashbinView)
			expect(el).toBeInstanceOf(HTMLElement)
			expect(el.textContent).toBe('folder')
			expect(el.title).toBe('folder')
		})

		it('renders a node when original location is missing', () => {
			const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain' })
			const el: HTMLElement = originalLocation.render(node, trashbinView)
			expect(el).toBeInstanceOf(HTMLElement)
			expect(el.textContent).toBe('Unknown')
			expect(el.title).toBe('Unknown')
		})

		it('renders a node when original location is the root', () => {
			const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-original-location': 'a.txt' } })
			const el: HTMLElement = originalLocation.render(node, trashbinView)
			expect(el).toBeInstanceOf(HTMLElement)
			expect(el.textContent).toBe('All files')
			expect(el.title).toBe('All files')
		})
	})

	describe('column: deleted time', () => {
		it('has id set', () => {
			expect(deleted.id).toBe('files_trashbin--deleted')
		})

		it('has title set', () => {
			expect(deleted.title).toBe('Deleted')
		})

		it('correctly sorts nodes by deleted time', () => {
			const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': 1741684522 } })
			const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': 1741684422 } })

			expect(deleted.sort).toBeTypeOf('function')
			expect(deleted.sort!(nodeA, nodeB)).toBeLessThan(0)
			expect(deleted.sort!(nodeB, nodeA)).toBeGreaterThan(0)
		})

		it('correctly sorts nodes by deleted time and falls back to mtime', () => {
			const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': 1741684522 } })
			const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', mtime: new Date(1741684422000) })

			expect(deleted.sort).toBeTypeOf('function')
			expect(deleted.sort!(nodeA, nodeB)).toBeLessThan(0)
			expect(deleted.sort!(nodeB, nodeA)).toBeGreaterThan(0)
		})

		it('correctly sorts nodes even if no deletion date is provided', () => {
			const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain' })
			const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', mtime: new Date(1741684422000) })

			expect(deleted.sort).toBeTypeOf('function')
			expect(deleted.sort!(nodeA, nodeB)).toBeGreaterThan(0)
			expect(deleted.sort!(nodeB, nodeA)).toBeLessThan(0)
		})

		describe('rendering', () => {
			afterAll(() => {
				vi.useRealTimers()
			})

			beforeEach(() => {
				vi.useFakeTimers({ now: 1741684582000 })
			})

			it('renders a node with deletion date', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deletion-time': 1741684522 } })
				const el: HTMLElement = deleted.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toBe('a minute ago')
				expect(el.title).toBe('March 11, 2025 9:15 AM')
			})

			it('renders a node when deletion date is missing and falls back to mtime', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', mtime: new Date(1741684522000) })
				const el: HTMLElement = deleted.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toBe('a minute ago')
				expect(el.title).toBe('March 11, 2025 9:15 AM')
			})

			it('renders a node when deletion date is missing', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain' })
				const el: HTMLElement = deleted.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toBe('A long time ago')
			})
		})

		describe('column: deleted by', () => {
			it('has id set', () => {
				expect(deletedBy.id).toBe('files_trashbin--deleted-by')
			})

			it('has title set', () => {
				expect(deletedBy.title).toBe('Deleted by')
			})

			it('correctly sorts nodes by user-id of deleting user', () => {
				const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'zzz' } })
				const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'aaa' } })

				expect(deletedBy.sort).toBeTypeOf('function')
				expect(deletedBy.sort!(nodeA, nodeB)).toBeGreaterThan(0)
				expect(deletedBy.sort!(nodeB, nodeA)).toBeLessThan(0)
			})

			it('correctly sorts nodes by display name of deleting user', () => {
				const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-display-name': 'zzz' } })
				const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-display-name': 'aaa' } })

				expect(deletedBy.sort).toBeTypeOf('function')
				expect(deletedBy.sort!(nodeA, nodeB)).toBeGreaterThan(0)
				expect(deletedBy.sort!(nodeB, nodeA)).toBeLessThan(0)
			})

			it('correctly sorts nodes by display name of deleting user before user id', () => {
				const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-display-name': '000', 'trashbin-deleted-by-id': 'zzz' } })
				const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-display-name': 'aaa', 'trashbin-deleted-by-id': '999' } })

				expect(deletedBy.sort).toBeTypeOf('function')
				expect(deletedBy.sort!(nodeA, nodeB)).toBeLessThan(0)
				expect(deletedBy.sort!(nodeB, nodeA)).toBeGreaterThan(0)
			})

			it('correctly sorts nodes even when one is missing', () => {
				const nodeA = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'aaa' } })
				const nodeB = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'zzz' } })
				const nodeC = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/b.txt', mime: 'text/plain' })

				expect(deletedBy.sort).toBeTypeOf('function')
				// aaa is less then "Unknown"
				expect(deletedBy.sort!(nodeA, nodeC)).toBeLessThan(0)
				// zzz is greater than "Unknown"
				expect(deletedBy.sort!(nodeB, nodeC)).toBeGreaterThan(0)
			})

			it('renders a node with deleting user', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'user-id' } })
				const el: HTMLElement = deletedBy.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toMatch(/\suser-id\s/)
			})

			it('renders a node with deleting user display name', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-display-name': 'user-name', 'trashbin-deleted-by-id': 'user-id' } })
				const el: HTMLElement = deletedBy.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toMatch(/\suser-name\s/)
			})

			it('renders a node even when information is missing', () => {
				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain' })
				const el: HTMLElement = deletedBy.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toBe('Unknown')
			})

			it('renders a node when current user is the deleting user', () => {
				vi.spyOn(ncAuth, 'getCurrentUser').mockImplementationOnce(() => ({
					uid: 'user-id',
					displayName: 'user-display-name',
					isAdmin: false,
				}))

				const node = new File({ owner: 'test', source: 'https://example.com/remote.php/dav/files/test/a.txt', mime: 'text/plain', attributes: { 'trashbin-deleted-by-id': 'user-id' } })
				const el: HTMLElement = deletedBy.render(node, trashbinView)
				expect(el).toBeInstanceOf(HTMLElement)
				expect(el.textContent).toBe('You')
			})
		})

	})

})