-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path15_CommandPattern.Tests.ps1
More file actions
342 lines (268 loc) · 10.7 KB
/
Copy path15_CommandPattern.Tests.ps1
File metadata and controls
342 lines (268 loc) · 10.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
BeforeAll {
. $PSScriptRoot/15_CommandPattern.ps1
}
Describe 'CryptoCommand Base Class' {
It 'Should throw NotImplementedException for Execute' {
$cmd = [CryptoCommand]::new()
{ $cmd.Execute() } | Should -Throw -ExceptionType ([System.NotImplementedException])
}
It 'Should throw NotImplementedException for Undo' {
$cmd = [CryptoCommand]::new()
{ $cmd.Undo() } | Should -Throw -ExceptionType ([System.NotImplementedException])
}
It 'Should have an 8-character ID' {
$cmd = [CryptoCommand]::new()
$cmd.Id | Should -BeOfType [string]
$cmd.Id.Length | Should -Be 8
}
It 'Should have CreatedAt timestamp' {
$cmd = [CryptoCommand]::new()
$cmd.CreatedAt | Should -BeOfType [datetime]
$cmd.CreatedAt | Should -BeLessOrEqual ([datetime]::UtcNow)
}
It 'Should default Executed to false' {
$cmd = [CryptoCommand]::new()
$cmd.Executed | Should -Be $false
}
It 'Should default Result to null' {
$cmd = [CryptoCommand]::new()
$cmd.Result | Should -BeNullOrEmpty
}
}
Describe 'NoopCommand' {
It 'Should execute successfully' {
$cmd = [NoopCommand]::new('test1')
$cmd.Execute()
$cmd.Executed | Should -Be $true
$cmd.Result | Should -Be 'executed:test1'
}
It 'Should undo successfully' {
$cmd = [NoopCommand]::new('test2')
$cmd.Execute()
$cmd.Undo()
$cmd.Executed | Should -Be $false
}
It 'Should describe with label' {
$cmd = [NoopCommand]::new('mylabel')
$cmd.Describe() | Should -Be 'Noop[mylabel]'
}
}
Describe 'FailingCommand' {
It 'Should throw on Execute' {
$cmd = [FailingCommand]::new()
{ $cmd.Execute() } | Should -Throw -ExceptionType ([System.InvalidOperationException])
}
It 'Should not throw on Undo' {
$cmd = [FailingCommand]::new()
{ $cmd.Undo() } | Should -Not -Throw
}
It 'Should describe correctly' {
$cmd = [FailingCommand]::new()
$cmd.Describe() | Should -Be 'FailingCommand'
}
}
Describe 'HashFileCommand' {
BeforeAll {
$script:testFile = Join-Path $TestDrive 'test.txt'
'Hello, World!' | Out-File -FilePath $script:testFile -NoNewline -Encoding utf8
}
It 'Should compute SHA-256 hash of file' {
$cmd = [HashFileCommand]::new($script:testFile)
$cmd.Execute()
$cmd.Executed | Should -Be $true
$cmd.Result | Should -Not -BeNullOrEmpty
$cmd.Result | Should -BeOfType [string]
# Expected hash for "Hello, World!"
$cmd.Result | Should -Match '^[a-f0-9]{64}$'
}
It 'Should throw FileNotFoundException for missing file' {
$cmd = [HashFileCommand]::new('C:\NonExistent\file.txt')
{ $cmd.Execute() } | Should -Throw -ExceptionType ([System.IO.FileNotFoundException])
}
It 'Should have no-op Undo' {
$cmd = [HashFileCommand]::new($script:testFile)
$cmd.Execute()
$result1 = $cmd.Result
{ $cmd.Undo() } | Should -Not -Throw
$cmd.Result | Should -Be $result1 # Result unchanged after Undo
}
It 'Should describe with file path' {
$cmd = [HashFileCommand]::new($script:testFile)
$cmd.Describe() | Should -Match 'HashFile\[.*test\.txt\]'
}
It 'Should produce consistent hash for same content' {
$cmd1 = [HashFileCommand]::new($script:testFile)
$cmd1.Execute()
$cmd2 = [HashFileCommand]::new($script:testFile)
$cmd2.Execute()
$cmd1.Result | Should -Be $cmd2.Result
}
It 'Should produce different hash for different content' {
$file2 = Join-Path $TestDrive 'test2.txt'
'Different content' | Out-File -FilePath $file2 -NoNewline -Encoding utf8
$cmd1 = [HashFileCommand]::new($script:testFile)
$cmd1.Execute()
$cmd2 = [HashFileCommand]::new($file2)
$cmd2.Execute()
$cmd1.Result | Should -Not -Be $cmd2.Result
}
}
Describe 'CryptoCommandQueue - Basic Operations' {
It 'Should create empty queue' {
$queue = [CryptoCommandQueue]::new()
$queue.PendingCount() | Should -Be 0
$queue.CompletedCount() | Should -Be 0
}
It 'Should enqueue commands' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('cmd1')
$cmd2 = [NoopCommand]::new('cmd2')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.PendingCount() | Should -Be 2
$queue.CompletedCount() | Should -Be 0
}
It 'Should execute commands in order' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('first')
$cmd2 = [NoopCommand]::new('second')
$cmd3 = [NoopCommand]::new('third')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
$queue.RunAll()
$queue.PendingCount() | Should -Be 0
$queue.CompletedCount() | Should -Be 3
$cmd1.Executed | Should -Be $true
$cmd2.Executed | Should -Be $true
$cmd3.Executed | Should -Be $true
}
}
Describe 'CryptoCommandQueue - UndoAll on Failure' {
It 'Should trigger UndoAll when command fails' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('before-fail')
$cmd2 = [FailingCommand]::new()
$cmd3 = [NoopCommand]::new('after-fail')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
{ $queue.RunAll() } | Should -Throw
# First command was executed but undone
$cmd1.Executed | Should -Be $false
# Third command never executed
$cmd3.Executed | Should -Be $false
# Queue and history should be empty after undo
$queue.CompletedCount() | Should -Be 0
}
It 'Should undo commands in reverse order' {
$queue = [CryptoCommandQueue]::new()
# Create commands that track undo order
$script:undoOrder = @()
$cmd1 = [NoopCommand]::new('first')
$originalUndo1 = $cmd1.Undo
$cmd1 | Add-Member -MemberType ScriptMethod -Name Undo -Value {
$script:undoOrder += 'first'
$this.Executed = $false
} -Force
$cmd2 = [NoopCommand]::new('second')
$cmd2 | Add-Member -MemberType ScriptMethod -Name Undo -Value {
$script:undoOrder += 'second'
$this.Executed = $false
} -Force
$cmd3 = [NoopCommand]::new('third')
$cmd3 | Add-Member -MemberType ScriptMethod -Name Undo -Value {
$script:undoOrder += 'third'
$this.Executed = $false
} -Force
$cmd4 = [FailingCommand]::new()
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
$queue.Enqueue($cmd4)
{ $queue.RunAll() } | Should -Throw
# Commands should be undone in reverse: third, second, first
$script:undoOrder | Should -Be @('third', 'second', 'first')
}
It 'Should handle mixed command types with failure' {
$testFile = Join-Path $TestDrive 'hash-test.txt'
'Test content' | Out-File -FilePath $testFile -NoNewline -Encoding utf8
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('noop1')
$cmd2 = [HashFileCommand]::new($testFile)
$cmd3 = [NoopCommand]::new('noop2')
$cmd4 = [FailingCommand]::new()
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
$queue.Enqueue($cmd4)
{ $queue.RunAll() } | Should -Throw
# All executed commands should be undone
$cmd1.Executed | Should -Be $false
$cmd2.Executed | Should -Be $true # HashFileCommand has no-op Undo
$cmd3.Executed | Should -Be $false
$queue.CompletedCount() | Should -Be 0
}
It 'Should not trigger UndoAll if all commands succeed' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('cmd1')
$cmd2 = [NoopCommand]::new('cmd2')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
{ $queue.RunAll() } | Should -Not -Throw
$cmd1.Executed | Should -Be $true
$cmd2.Executed | Should -Be $true
$queue.CompletedCount() | Should -Be 2
}
It 'Should clear pending queue after failure' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('before')
$cmd2 = [FailingCommand]::new()
$cmd3 = [NoopCommand]::new('after')
$cmd4 = [NoopCommand]::new('after2')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
$queue.Enqueue($cmd4)
{ $queue.RunAll() } | Should -Throw
# Pending commands after failure should remain in queue
# Actually, the implementation dequeues before executing, so failed command
# is removed and remaining commands stay in queue
$queue.PendingCount() | Should -Be 2 # cmd3 and cmd4 remain
}
}
Describe 'CryptoCommandQueue - HashFileCommand Integration' {
BeforeAll {
$script:file1 = Join-Path $TestDrive 'file1.txt'
$script:file2 = Join-Path $TestDrive 'file2.txt'
'Content 1' | Out-File -FilePath $script:file1 -NoNewline -Encoding utf8
'Content 2' | Out-File -FilePath $script:file2 -NoNewline -Encoding utf8
}
It 'Should execute multiple HashFileCommands' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [HashFileCommand]::new($script:file1)
$cmd2 = [HashFileCommand]::new($script:file2)
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.RunAll()
$cmd1.Result | Should -Not -BeNullOrEmpty
$cmd2.Result | Should -Not -BeNullOrEmpty
$cmd1.Result | Should -Not -Be $cmd2.Result
$queue.CompletedCount() | Should -Be 2
}
It 'Should handle HashFileCommand failure in queue' {
$queue = [CryptoCommandQueue]::new()
$cmd1 = [NoopCommand]::new('before-hash')
$cmd2 = [HashFileCommand]::new('C:\NonExistent\missing.txt')
$cmd3 = [NoopCommand]::new('after-hash')
$queue.Enqueue($cmd1)
$queue.Enqueue($cmd2)
$queue.Enqueue($cmd3)
{ $queue.RunAll() } | Should -Throw
# First command should be undone
$cmd1.Executed | Should -Be $false
# Third command never executed
$cmd3.Executed | Should -Be $false
$queue.CompletedCount() | Should -Be 0
}
}