qubit-fs-testkit 0.9.2

Reusable provider contract checks for qubit-fs implementations
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Copy cancellation evidence and explicit writer recovery.

use std::future::Future;
use std::future::poll_fn;
use std::task::Poll;

use qubit_fs::copy::AsyncCopyOperation;
use qubit_fs::copy::AsyncCopyOperationState;
use qubit_fs::copy::CopyFailureState;
use qubit_fs::error::FsErrorKind;
use qubit_fs::metadata::FileSystemCapability;
use qubit_fs::path::Path;
use qubit_fs::write::AsyncWriterRecovery;
use qubit_fs::write::WriteAbortOutcome;
use qubit_fs::write::WriterState;

use crate::AsyncCopyCancellationStage;
use crate::AsyncFileSystemContractSuite;
use crate::AsyncFileSystemFixture;
use crate::ContractCheckId;
use crate::ContractCheckOutcome;
use crate::ContractFailure;
use crate::CopyCancellationProbe;
use crate::FixtureSupport;
use crate::internal::probe_disarm_guard::ProbeDisarmGuard;
use crate::internal::verify_condition;

impl AsyncFileSystemContractSuite<'_> {
    /// Executes only the selected stage and records no sibling outcomes.
    ///
    /// # Parameters
    ///
    /// - `id`: Copy-cancellation check identity selecting the lifecycle stage.
    ///
    /// # Errors
    ///
    /// Returns a failure when the selected stage cannot be prepared or
    /// verified.
    pub(super) async fn check_copy_cancellation_item(&mut self, id: ContractCheckId) -> Result<(), ContractFailure> {
        let stage = match id {
            ContractCheckId::AsyncCopyCancelNativeAttempt => AsyncCopyCancellationStage::NativeAttempt,
            ContractCheckId::AsyncCopyCancelReader => AsyncCopyCancellationStage::Reader,
            ContractCheckId::AsyncCopyCancelWriter => AsyncCopyCancellationStage::Writer,
            ContractCheckId::AsyncCopyCancelCommit => AsyncCopyCancellationStage::Commit,
            _ => {
                return Err(ContractFailure::message_only("selected check is not copy cancellation").at(id));
            }
        };
        self.context.begin_check(id);
        if !self.capable(FileSystemCapability::Copy) {
            self.context.record_check(
                id,
                Some(FileSystemCapability::Copy),
                ContractCheckOutcome::NotApplicable {
                    reason: "basic copy capability is unavailable".to_owned(),
                },
            );
            return Ok(());
        }
        let relative = self.context.relative_name(&format!("async-copy-cancel-{stage:?}"));
        let prepared = self
            .fixture
            .prepare_copy_cancellation(stage, &relative)
            .await
            .map_err(|error| ContractFailure::with_source("copy cancellation preparation failed", error).at(id))?;
        match prepared {
            FixtureSupport::Supported(probe) => self.run_cancellation_probe(stage, probe).await?,
            FixtureSupport::Unsupported => self.context.record_check(
                id,
                Some(FileSystemCapability::Copy),
                ContractCheckOutcome::SkippedOptional {
                    reason: "fixture has no stage acknowledgement probe".to_owned(),
                },
            ),
        }
        Ok(())
    }

    /// Stops execution before releasing the gate, then validates recovery
    /// facts.
    ///
    /// # Parameters
    ///
    /// * `stage` - Cancellation stage whose acknowledgement must be reached.
    /// * `probe` - Fixture probe that gates the selected copy stage.
    ///
    /// # Errors
    ///
    /// Returns a [`ContractFailure`] if the copy cannot be observed, cancelled,
    /// or recovered according to the stage contract.
    async fn run_cancellation_probe(
        &mut self,
        stage: AsyncCopyCancellationStage,
        probe: Box<dyn CopyCancellationProbe>,
    ) -> Result<(), ContractFailure> {
        let id = cancellation_check_id(stage);
        let (source, target, options) = probe.case().clone().into_parts();
        self.context.record_created(source.clone());
        self.context.record_created(target.clone());
        let mut guard = ProbeDisarmGuard::new(|| probe.disarm(), &mut self.context.run.cleanup.failures);
        let source_before = observe_file(self.fixture, &source, id)
            .await?
            .ok_or_else(|| ContractFailure::message_only("copy cancellation source is absent").at(id))?;
        let target_before = observe_file(self.fixture, &target, id).await?;
        let mut operation = self
            .fixture
            .file_system()
            .begin_copy(source.clone(), target.clone(), options)
            .map_err(|error| {
                ContractFailure::with_owned_source(
                    "copy cancellation request admission failed",
                    crate::ContractAsyncCopyFailure::new(error, None),
                )
                .at(id)
            })?;
        drop(operation.execute());
        verify_condition(
            operation.state() == AsyncCopyOperationState::Ready,
            id,
            "unpolled copy execution changed state",
        )?;
        verify_condition(!operation.has_recovery(), id, "unpolled copy acquired a writer")?;
        let mut execution_failure = None;
        let mut execution = Box::pin(operation.execute());
        let reached = poll_fn(|context| match execution.as_mut().poll(context) {
            Poll::Pending => probe.poll_reached(context).map(|result| {
                result.map_err(|error| ContractFailure::with_source("copy stage acknowledgement failed", error).at(id))
            }),
            Poll::Ready(Ok(_)) => Poll::Ready(Err(ContractFailure::message_only(
                "copy completed before requested stage acknowledgement",
            )
            .at(id))),
            Poll::Ready(Err(error)) => {
                execution_failure = Some(error);
                Poll::Ready(Err(ContractFailure::message_only(
                    "copy failed before requested stage acknowledgement",
                )
                .at(id)))
            }
        })
        .await;
        drop(execution);
        let disarmed = guard.disarm();
        drop(guard);
        if let Some(error) = execution_failure {
            return Err(ContractFailure::with_owned_source(
                "copy failed before requested stage acknowledgement",
                crate::ContractAsyncCopyFailure::new(error, Some(operation)),
            )
            .at(id));
        }
        if let Err(error) = reached {
            return Err(retain_copy_operation(error, operation, id));
        }
        if let Err(error) = verify_condition(
            disarmed,
            id,
            "probe disarm failed; original cause is retained in cleanup",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        if let Err(error) = verify_condition(
            operation.state() == AsyncCopyOperationState::Failed(CopyFailureState::Indeterminate),
            id,
            "cancelled copy did not become indeterminate",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        let has_writer = matches!(
            stage,
            AsyncCopyCancellationStage::Writer | AsyncCopyCancellationStage::Commit
        );
        if let Err(error) = verify_condition(
            operation.has_recovery() == has_writer,
            id,
            "copy recovery writer responsibility differs",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        let repeated = match operation.execute().await {
            Err(error) => error,
            Ok(_) => {
                return Err(retain_copy_operation(
                    ContractFailure::message_only("cancelled copy executed twice").at(id),
                    operation,
                    id,
                ));
            }
        };
        if repeated.error().kind() != FsErrorKind::InvalidState || repeated.state() != CopyFailureState::Indeterminate {
            return Err(ContractFailure::with_owned_source(
                "repeated copy lost cancellation facts",
                crate::ContractAsyncCopyFailure::new(repeated, Some(operation)),
            )
            .at(id));
        }
        if let Err(error) = verify_condition(
            repeated.partial_stats().bytes <= source_before.len() as u64,
            id,
            "copy progress exceeds independently observed source bytes",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        let repeated_again = match operation.execute().await {
            Err(error) => error,
            Ok(_) => {
                return Err(retain_copy_operation(
                    ContractFailure::message_only("cancelled copy executed a third time").at(id),
                    operation,
                    id,
                ));
            }
        };
        if repeated_again.error().kind() != FsErrorKind::InvalidState
            || repeated_again.state() != repeated.state()
            || repeated_again.partial_stats() != repeated.partial_stats()
        {
            return Err(ContractFailure::with_owned_source(
                "repeated copy changed retained recovery facts",
                crate::ContractAsyncCopyFailure::new(repeated_again, Some(operation)),
            )
            .at(id));
        }
        if let Err(error) = verify_condition(
            operation.state() == AsyncCopyOperationState::Failed(CopyFailureState::Indeterminate),
            id,
            "repeat changed cancelled copy state",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        if let Err(error) = verify_condition(
            operation.has_recovery() == has_writer,
            id,
            "repeat lost copy recovery writer",
        ) {
            return Err(retain_copy_operation(error, operation, id));
        }
        if let Some(recovery) = operation.take_recovery() {
            let mut writer = match recovery {
                AsyncWriterRecovery::Opened(writer) => *writer,
                AsyncWriterRecovery::Rejected(recovery) => {
                    return Err(ContractFailure::with_owned_source(
                        "cancellation probe retained a rejected provider identity",
                        crate::ContractWriterFailure::new(
                            ContractFailure::message_only("validated writer required by this stage probe"),
                            recovery,
                        ),
                    )
                    .at(id));
                }
            };
            let outcome = match writer.abort_async().await {
                Ok(outcome) => outcome,
                Err(error) => {
                    return Err(ContractFailure::with_owned_source(
                        "copy recovery abort failed",
                        crate::ContractWriterFailure::new(error, writer),
                    )
                    .at(id));
                }
            };
            let expected = match outcome {
                WriteAbortOutcome::NotPublished => WriterState::Aborted,
                WriteAbortOutcome::Published => WriterState::Published,
                WriteAbortOutcome::Indeterminate => WriterState::Indeterminate,
            };
            verify_condition(
                writer.state() == expected,
                id,
                "copy recovery state differs from abort outcome",
            )?;
            let target_after = observe_file(self.fixture, &target, id).await?;
            match outcome {
                WriteAbortOutcome::NotPublished => verify_condition(
                    target_after == target_before,
                    id,
                    "NotPublished copy recovery changed target",
                )?,
                WriteAbortOutcome::Published => verify_condition(
                    target_after.is_some(),
                    id,
                    "Published copy recovery target is not independently observable",
                )?,
                WriteAbortOutcome::Indeterminate => {}
            }
        }
        let source_after = observe_file(self.fixture, &source, id).await?;
        verify_condition(
            source_after.as_deref() == Some(source_before.as_slice()),
            id,
            "cancelled copy changed source",
        )?;
        self.context
            .record_check(id, Some(FileSystemCapability::Copy), ContractCheckOutcome::Passed);
        Ok(())
    }
}

/// Retains the copy operation alongside the diagnostic that interrupted a
/// cancellation observation.
///
/// # Parameters
///
/// - `failure`: Diagnostic produced while checking the cancellation stage.
/// - `operation`: Copy operation retained for recovery or inspection.
/// - `id`: Check identity attached to the resulting failure.
///
/// # Returns
///
/// A failure that owns the operation and its diagnostic.
fn retain_copy_operation(
    failure: ContractFailure,
    operation: AsyncCopyOperation,
    id: ContractCheckId,
) -> ContractFailure {
    ContractFailure::with_owned_source(
        "copy cancellation diagnostic failed; operation retained",
        crate::ContractWriterFailure::new(failure, operation),
    )
    .at(id)
}

/// Returns the stable identity for one cancellation stage.
///
/// # Parameters
///
/// - `stage`: Copy cancellation lifecycle stage to identify.
///
/// # Returns
///
/// The registered check identity associated with `stage`.
#[inline]
const fn cancellation_check_id(stage: AsyncCopyCancellationStage) -> ContractCheckId {
    match stage {
        AsyncCopyCancellationStage::NativeAttempt => ContractCheckId::AsyncCopyCancelNativeAttempt,
        AsyncCopyCancellationStage::Reader => ContractCheckId::AsyncCopyCancelReader,
        AsyncCopyCancellationStage::Writer => ContractCheckId::AsyncCopyCancelWriter,
        AsyncCopyCancellationStage::Commit => ContractCheckId::AsyncCopyCancelCommit,
    }
}

/// Requires independent absence or complete file-content evidence.
///
/// # Parameters
///
/// * `fixture` - Fixture providing independent filesystem observations.
/// * `path` - File path whose existence and contents are observed.
/// * `id` - Contract check identity attached to observation failures.
///
/// # Returns
///
/// `Ok(None)` when the path is independently confirmed absent, or `Ok(Some)`
/// with its complete contents when present.
///
/// # Errors
///
/// Returns a [`ContractFailure`] when an observation fails or the fixture does
/// not support the evidence required for the path.
async fn observe_file(
    fixture: &dyn AsyncFileSystemFixture,
    path: &Path,
    id: ContractCheckId,
) -> Result<Option<Vec<u8>>, ContractFailure> {
    match fixture
        .exists_out_of_band(path)
        .await
        .map_err(|error| ContractFailure::with_source("copy independent existence observation failed", error).at(id))?
    {
        FixtureSupport::Supported(false) => return Ok(None),
        FixtureSupport::Supported(true) => {}
        FixtureSupport::Unsupported => {
            return Err(ContractFailure::message_only("copy probe has no independent existence evidence").at(id));
        }
    }
    match fixture
        .read_file(path)
        .await
        .map_err(|error| ContractFailure::with_source("copy independent byte observation failed", error).at(id))?
    {
        FixtureSupport::Supported(bytes) => Ok(Some(bytes)),
        FixtureSupport::Unsupported => {
            Err(ContractFailure::message_only("copy probe has no independent byte evidence").at(id))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::AsyncFileSystemContractSuite;
    use crate::ContractCheckId;
    use crate::common::AsyncMemoryFixture;
    use crate::common::async_memory_file_system::run_controlled;

    #[test]
    fn test_rejects_a_non_cancellation_check_identity() {
        let fixture = AsyncMemoryFixture::new();
        run_controlled(async {
            let mut suite = AsyncFileSystemContractSuite::new(&fixture);
            let result = suite.check_copy_cancellation_item(ContractCheckId::ReadBasic).await;
            assert!(result.is_err(), "non-cancellation check identity was accepted");
        });
    }

    #[test]
    fn test_records_copy_cancellation_as_inapplicable_without_copy_capability() {
        let fixture = AsyncMemoryFixture::without_core_capabilities();
        run_controlled(async {
            let mut suite = AsyncFileSystemContractSuite::new(&fixture);
            suite
                .context
                .prepare_phase(ContractCheckId::AsyncCopyCancelNativeAttempt.contract(), true);
            suite
                .check_copy_cancellation_item(ContractCheckId::AsyncCopyCancelNativeAttempt)
                .await
                .expect("missing copy capability makes the probe inapplicable");
            let check = suite
                .report()
                .checks()
                .iter()
                .find(|check| check.id() == ContractCheckId::AsyncCopyCancelNativeAttempt)
                .expect("inapplicable check must be recorded");
            assert!(matches!(
                check.outcome(),
                crate::ContractCheckOutcome::NotApplicable { .. }
            ));
        });
        assert!(fixture.is_empty(), "inapplicable probe must not create resources");
    }
}