@@ -7,31 +7,78 @@ import { strict as assert } from "node:assert";
77
88import type { ISharedMatrix , SharedMatrix } from "../matrix.js" ;
99
10- /**
11- * @internal
12- */
10+ import { TestConsumer } from "./testconsumer.js" ;
11+
1312export class SharedMatrixOracle {
14- constructor ( private readonly shared : ISharedMatrix ) {
15- this . shared . on ( "conflict" , ( row , col , currentValue , conflictingValue , target ) => {
16- if ( row < this . shared . rowCount && col < this . shared . colCount ) {
17- assert (
18- this . shared . isSetCellConflictResolutionPolicyFWW ( ) ,
19- "Conflict should only fire in FWW mode" ,
20- ) ;
13+ private readonly testConsumer : TestConsumer ;
14+ private readonly conflictListener : (
15+ row : number ,
16+ col : number ,
17+ currentValue : unknown ,
18+ conflictingValue : unknown ,
19+ ) => void ;
20+
21+ constructor ( private readonly shared : SharedMatrix ) {
22+ this . testConsumer = new TestConsumer ( shared ) ;
23+
24+ this . conflictListener = ( row , col , currentValue , conflictingValue ) => {
25+ this . onConflict ( row , col , currentValue , conflictingValue ) ;
26+ } ;
27+
28+ this . shared . on ( "conflict" , this . conflictListener ) ;
29+ }
30+
31+ private onConflict (
32+ row : number ,
33+ col : number ,
34+ currentValue : unknown ,
35+ conflictingValue : unknown ,
36+ ) : void {
37+ assert . ok (
38+ this . shared . isSetCellConflictResolutionPolicyFWW ( ) ,
39+ "Conflict should only fire in FWW mode" ,
40+ ) ;
41+
42+ if ( row < this . shared . rowCount && col < this . shared . colCount ) {
43+ assert (
44+ this . shared . isSetCellConflictResolutionPolicyFWW ( ) ,
45+ "Conflict should only fire in FWW mode" ,
46+ ) ;
2147
22- const actual = this . shared . getCell ( row , col ) ;
48+ const actual = this . testConsumer . getCell ( row , col ) ;
2349
24- // The loser must be different
25- assert . notDeepStrictEqual ( currentValue , conflictingValue ) ;
50+ // The loser must be different
51+ assert . notDeepStrictEqual ( currentValue , conflictingValue ) ;
2652
27- // The cell contains the winner
28- assert . deepStrictEqual (
53+ // The cell contains the winner
54+ assert . deepStrictEqual (
55+ actual ,
56+ currentValue ,
57+ `Conflict mismatch at [${ row } ,${ col } ]: expected winner=${ currentValue } , actual=${ actual } with conflicting value=${ conflictingValue } ` ,
58+ ) ;
59+ }
60+ }
61+
62+ public validate ( ) : void {
63+ const rows = this . shared . rowCount ;
64+ const cols = this . shared . colCount ;
65+
66+ // Validate the entire matrix
67+ for ( let r = 0 ; r < rows ; r ++ ) {
68+ for ( let c = 0 ; c < cols ; c ++ ) {
69+ const expected = this . testConsumer . getCell ( r , c ) ;
70+ const actual = this . shared . getCell ( r , c ) ;
71+ assert . strictEqual (
2972 actual ,
30- currentValue ,
31- `Conflict mismatch at [${ row } ,${ col } ]: expected winner= ${ currentValue } , actual=${ actual } with conflicting value= ${ conflictingValue } ` ,
73+ expected ,
74+ `Mismatch at [${ r } ,${ c } ]: expected=" ${ expected } " , actual=" ${ actual } " ` ,
3275 ) ;
3376 }
34- } ) ;
77+ }
78+ }
79+
80+ public dispose ( ) : void {
81+ this . shared . off ( "conflict" , this . conflictListener ) ;
3582 }
3683}
3784
@@ -41,3 +88,11 @@ export class SharedMatrixOracle {
4188export interface IChannelWithOracles extends SharedMatrix {
4289 matrixOracle : SharedMatrixOracle ;
4390}
91+
92+ /**
93+ * Type guard for SharedMatrix with an oracle
94+ * @internal
95+ */
96+ export function hasSharedMatrixOracle ( s : ISharedMatrix ) : s is IChannelWithOracles {
97+ return "matrixOracle" in s && s . matrixOracle instanceof SharedMatrixOracle ;
98+ }
0 commit comments