@shaztech/video-cutter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,250 @@
1
+ /*
2
+ * Licensed under the Apache License, Version 2.0 (the "License");
3
+ * you may not use this file except in compliance with the License.
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+
15
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
16
+
17
+ import fs from 'fs'
18
+ import inquirer from 'inquirer'
19
+ import { Command } from 'commander'
20
+ import { getVideoDuration, createCountSegments, createTimeSegments } from '../src/core.js'
21
+ import { processVideo, setupCli } from '../index.js'
22
+
23
+ vi.mock('child_process', () => ({
24
+ execSync: vi.fn(), // doesn't throw = ffmpeg/ffprobe available
25
+ spawn: vi.fn()
26
+ }))
27
+
28
+ vi.mock('../src/core.js', () => ({
29
+ getVideoDuration: vi.fn(),
30
+ createCountSegments: vi.fn(),
31
+ createTimeSegments: vi.fn()
32
+ }))
33
+
34
+ vi.mock('commander', () => {
35
+ const mockProgram = {
36
+ name: vi.fn().mockReturnThis(),
37
+ description: vi.fn().mockReturnThis(),
38
+ version: vi.fn().mockReturnThis(),
39
+ requiredOption: vi.fn().mockReturnThis(),
40
+ addOption: vi.fn().mockReturnThis(),
41
+ option: vi.fn().mockReturnThis(),
42
+ action: vi.fn().mockReturnThis(),
43
+ parse: vi.fn()
44
+ }
45
+ // eslint-disable-next-line prefer-arrow-callback
46
+ const MockOption = vi.fn(function MockOption () { return { conflicts: vi.fn().mockReturnThis() } })
47
+ // eslint-disable-next-line prefer-arrow-callback
48
+ return { Command: vi.fn(function MockCommand () { return mockProgram }), Option: MockOption }
49
+ })
50
+
51
+ describe('processVideo', () => {
52
+ beforeEach(() => {
53
+ vi.clearAllMocks()
54
+ vi.spyOn(console, 'log').mockImplementation(() => {})
55
+ vi.spyOn(console, 'warn').mockImplementation(() => {})
56
+ vi.spyOn(console, 'error').mockImplementation(() => {})
57
+ })
58
+ afterEach(() => vi.restoreAllMocks())
59
+
60
+ it('exits when input file does not exist', async () => {
61
+ vi.spyOn(fs, 'existsSync').mockReturnValueOnce(false)
62
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
63
+ await expect(processVideo({ input: 'missing.mp4', segments: 2, output: '/out' })).rejects.toThrow('exit')
64
+ expect(exitSpy).toHaveBeenCalledWith(1)
65
+ })
66
+
67
+ it('creates default output directory path when none provided', async () => {
68
+ vi.spyOn(fs, 'existsSync')
69
+ .mockReturnValueOnce(true) // input file exists
70
+ .mockReturnValueOnce(false) // output dir does not exist
71
+ vi.spyOn(fs, 'mkdirSync').mockImplementation(() => {})
72
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
73
+ vi.mocked(createCountSegments).mockResolvedValue()
74
+
75
+ await processVideo({ input: 'video.mp4', segments: 2 })
76
+
77
+ expect(fs.mkdirSync).toHaveBeenCalledWith(expect.stringContaining('output'), { recursive: true })
78
+ })
79
+
80
+ it('creates output directory when it does not exist', async () => {
81
+ vi.spyOn(fs, 'existsSync')
82
+ .mockReturnValueOnce(true) // input file exists
83
+ .mockReturnValueOnce(false) // output dir does not exist
84
+ vi.spyOn(fs, 'mkdirSync').mockImplementation(() => {})
85
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
86
+ vi.mocked(createCountSegments).mockResolvedValue()
87
+
88
+ await processVideo({ input: 'video.mp4', segments: 2, output: '/out' })
89
+
90
+ expect(fs.mkdirSync).toHaveBeenCalledWith('/out', { recursive: true })
91
+ })
92
+
93
+ it('exits when video duration cannot be determined (falsy)', async () => {
94
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
95
+ vi.mocked(getVideoDuration).mockResolvedValue(0)
96
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
97
+ await expect(processVideo({ input: 'video.mp4', segments: 2, output: '/out' })).rejects.toThrow('exit')
98
+ expect(exitSpy).toHaveBeenCalledWith(1)
99
+ })
100
+
101
+ it('exits when getVideoDuration throws', async () => {
102
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
103
+ vi.mocked(getVideoDuration).mockRejectedValue(new Error('ffprobe failed'))
104
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
105
+ await expect(processVideo({ input: 'video.mp4', segments: 2, output: '/out' })).rejects.toThrow('exit')
106
+ expect(exitSpy).toHaveBeenCalledWith(1)
107
+ })
108
+
109
+ describe('time-based segmentation (--duration)', () => {
110
+ it('warns about stream copy mode when not re-encoding', async () => {
111
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
112
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
113
+ vi.mocked(createTimeSegments).mockResolvedValue()
114
+
115
+ await processVideo({ input: 'video.mp4', duration: 60, output: '/out' })
116
+
117
+ expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('stream copy mode'))
118
+ expect(createTimeSegments).toHaveBeenCalledWith('video.mp4', 120, 60, '/out', false, false)
119
+ })
120
+
121
+ it('does not warn when re-encoding', async () => {
122
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
123
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
124
+ vi.mocked(createTimeSegments).mockResolvedValue()
125
+
126
+ await processVideo({ input: 'video.mp4', duration: 60, output: '/out', reEncode: true })
127
+
128
+ expect(console.warn).not.toHaveBeenCalled()
129
+ expect(createTimeSegments).toHaveBeenCalledWith('video.mp4', 120, 60, '/out', false, true)
130
+ })
131
+
132
+ it('prompts user for short segments (<30s) and proceeds when confirmed', async () => {
133
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
134
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
135
+ vi.mocked(createTimeSegments).mockResolvedValue()
136
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })
137
+
138
+ await processVideo({ input: 'video.mp4', duration: 10, output: '/out' })
139
+
140
+ expect(inquirer.prompt).toHaveBeenCalled()
141
+ expect(createTimeSegments).toHaveBeenCalled()
142
+ })
143
+
144
+ it('exits when user cancels short segment prompt', async () => {
145
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
146
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
147
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: false })
148
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
149
+
150
+ await expect(processVideo({ input: 'video.mp4', duration: 10, output: '/out' })).rejects.toThrow('exit')
151
+ expect(exitSpy).toHaveBeenCalledWith(0)
152
+ })
153
+ })
154
+
155
+ describe('count-based segmentation (--segments)', () => {
156
+ it('warns about stream copy mode when segments >= 30s', async () => {
157
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
158
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
159
+ vi.mocked(createCountSegments).mockResolvedValue()
160
+
161
+ await processVideo({ input: 'video.mp4', segments: 2, output: '/out' })
162
+
163
+ expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('stream copy mode'))
164
+ expect(createCountSegments).toHaveBeenCalledWith('video.mp4', 120, 2, '/out', false, false)
165
+ })
166
+
167
+ it('does not warn when re-encoding with segments >= 30s', async () => {
168
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
169
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
170
+ vi.mocked(createCountSegments).mockResolvedValue()
171
+
172
+ await processVideo({ input: 'video.mp4', segments: 2, output: '/out', reEncode: true })
173
+
174
+ expect(console.warn).not.toHaveBeenCalled()
175
+ })
176
+
177
+ it('warns about stream copy mode when segments < 30s and not re-encoding', async () => {
178
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
179
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
180
+ vi.mocked(createCountSegments).mockResolvedValue()
181
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })
182
+
183
+ await processVideo({ input: 'video.mp4', segments: 10, output: '/out' })
184
+
185
+ expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('stream copy mode'))
186
+ })
187
+
188
+ it('does not warn when re-encoding with short count segments', async () => {
189
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
190
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
191
+ vi.mocked(createCountSegments).mockResolvedValue()
192
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })
193
+
194
+ await processVideo({ input: 'video.mp4', segments: 10, output: '/out', reEncode: true })
195
+
196
+ expect(console.warn).not.toHaveBeenCalled()
197
+ expect(createCountSegments).toHaveBeenCalled()
198
+ })
199
+
200
+ it('prompts user for short segments and proceeds when confirmed', async () => {
201
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
202
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
203
+ vi.mocked(createCountSegments).mockResolvedValue()
204
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })
205
+
206
+ await processVideo({ input: 'video.mp4', segments: 10, output: '/out' })
207
+
208
+ expect(inquirer.prompt).toHaveBeenCalled()
209
+ expect(createCountSegments).toHaveBeenCalled()
210
+ })
211
+
212
+ it('exits when user cancels short segment prompt', async () => {
213
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
214
+ vi.mocked(getVideoDuration).mockResolvedValue(60)
215
+ vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: false })
216
+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
217
+
218
+ await expect(processVideo({ input: 'video.mp4', segments: 10, output: '/out' })).rejects.toThrow('exit')
219
+ expect(exitSpy).toHaveBeenCalledWith(0)
220
+ })
221
+
222
+ it('passes verify and reEncode flags correctly', async () => {
223
+ vi.spyOn(fs, 'existsSync').mockReturnValue(true)
224
+ vi.mocked(getVideoDuration).mockResolvedValue(120)
225
+ vi.mocked(createCountSegments).mockResolvedValue()
226
+
227
+ await processVideo({ input: 'video.mp4', segments: 2, output: '/out', verify: true, reEncode: true })
228
+
229
+ expect(createCountSegments).toHaveBeenCalledWith('video.mp4', 120, 2, '/out', true, true)
230
+ })
231
+ })
232
+ })
233
+
234
+ describe('setupCli', () => {
235
+ beforeEach(() => {
236
+ vi.clearAllMocks()
237
+ vi.spyOn(console, 'log').mockImplementation(() => {})
238
+ })
239
+ afterEach(() => vi.restoreAllMocks())
240
+
241
+ it('configures CLI with correct name, version, and options then calls parse', () => {
242
+ setupCli()
243
+ const program = vi.mocked(Command).mock.results[0].value
244
+ expect(program.name).toHaveBeenCalledWith('video-cutter')
245
+ expect(program.version).toHaveBeenCalledWith('1.0.0')
246
+ expect(program.requiredOption).toHaveBeenCalledWith('-i, --input <path>', expect.any(String))
247
+ expect(program.action).toHaveBeenCalledWith(processVideo)
248
+ expect(program.parse).toHaveBeenCalled()
249
+ })
250
+ })
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from 'vitest/config'
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ coverage: {
8
+ provider: 'v8',
9
+ reporter: ['text', 'html', 'lcov'],
10
+ exclude: ['**/node_modules/**', '**/dist/**', '**/*.spec.js', 'vitest.config.js']
11
+ }
12
+ }
13
+ })