All files / json-crdt-extensions/quill-delta/__tests__ QuillDeltaFuzzer.ts

96.36% Statements 106/110
88.88% Branches 24/27
93.75% Functions 15/16
98.11% Lines 104/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1832x 2x 2x 2x   2x 2x           2x   350x 350x 350x     350x             350x             15000x 15000x 15000x       15000x 15000x 15000x 15000x 15000x 26777x 26777x 5463x   2751x 2751x 2751x     2712x 2712x 2712x     5463x 5463x   21314x   5449x 5449x 5449x     5310x 5310x 5310x     5268x 5268x 5268x     5287x 5287x 5287x     21314x   15000x                 8200x 8200x 8200x 8200x 8200x 8200x 4349x   8200x 8200x 4131x 4131x 2831x     8200x 8200x       8022x 8022x 8022x 8022x 8022x 8022x 4205x   8022x 8022x 3992x 3992x 2727x     8022x       5268x 5268x 5268x 5268x 5268x       5287x 5287x 5287x 5287x 5287x 5287x 3593x   5287x 5287x 5287x 5287x   5287x 5287x       13410x 13410x 13410x 13410x 26766x 26766x   8798x 8798x     8961x 8961x     9007x 9007x       13410x      
import Delta from 'quill-delta';
import {randomU32} from 'hyperdyperid/lib/randomU32';
import {Fuzzer} from '@jsonjoy.com/util/lib/Fuzzer';
import {isEmpty} from '@jsonjoy.com/util/lib/isEmpty';
import type {QuillDeltaAttributes, QuillDeltaOp, QuillDeltaOpInsert, QuillDeltaOpRetain, QuillTrace} from '../types';
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
import {removeErasures} from '../util';
 
export interface QuillDeltaFuzzerOptions {
  maxOperationsPerPatch: number;
}
 
export class QuillDeltaFuzzer {
  public options: QuillDeltaFuzzerOptions;
  public delta: Delta = new Delta([]);
  public patch: QuillDeltaOp[] = [];
  public transactions: QuillDeltaOp[][] = [];
 
  constructor(options: Partial<QuillDeltaFuzzerOptions> = {}) {
    this.options = {
      maxOperationsPerPatch: 3,
      ...options,
    };
  }
 
  public trace(): QuillTrace {
    return {
      contents: {ops: this.delta.ops as QuillDeltaOp[]},
      transactions: this.transactions,
    };
  }
 
  public applyPatch(): Delta {
    this.transactions.push(this.patch);
    this.delta = this.delta.compose(new Delta(this.patch));
    return this.delta;
  }
 
  public createPatch(): QuillDeltaOp[] {
    this.patch = [];
    let pos = 0;
    const length = this.delta.length();
    const opCount = randomU32(1, this.options.maxOperationsPerPatch);
    for (let i = 0; i < opCount; i++) {
      const remaining = length - pos;
      if (!remaining) {
        const op = Fuzzer.pick([
          () => {
            const [offset, patch] = this.createInsertTextOp(this.delta, pos);
            this.patch.push(...patch);
            pos += offset;
          },
          () => {
            const [offset, patch] = this.createInsertEmbedOp(this.delta, pos);
            this.patch.push(...patch);
            pos += offset;
          },
        ]);
        op();
        break;
      }
      const op = Fuzzer.pick([
        () => {
          const [offset, patch] = this.createInsertTextOp(this.delta, pos);
          this.patch.push(...patch);
          pos += offset;
        },
        () => {
          const [offset, patch] = this.createInsertEmbedOp(this.delta, pos);
          this.patch.push(...patch);
          pos += offset;
        },
        () => {
          const [offset, patch] = this.createDeleteOp(length, pos);
          this.patch.push(...patch);
          pos += offset;
        },
        () => {
          const [offset, patch] = this.createAnnotateOp(length, pos);
          this.patch.push(...patch);
          pos += offset;
        },
      ]);
      op();
    }
    return this.patch;
  }
 
  public createInsertOp(delta: Delta, pos: number): [offset: number, patch: QuillDeltaOp[]] {
    const [offset, patch] = this.createInsertTextOp(delta, pos);
    return [offset, patch];
  }
 
  public createInsertTextOp(delta: Delta, pos: number): [offset: number, patch: QuillDeltaOp[]] {
    const length = delta.length();
    const remaining = length - pos;
    const offset = !remaining ? 0 : randomU32(0, remaining);
    const text = RandomJson.genString(randomU32(1, 5));
    const patch: QuillDeltaOp[] = [];
    if (offset > 0) {
      patch.push({retain: offset});
    }
    const insertOp: QuillDeltaOpInsert = {insert: text};
    if (randomU32(0, 1)) {
      const attributes = removeErasures(this.createAttributes());
      if (attributes && !isEmpty(attributes)) {
        insertOp.attributes = attributes;
      }
    }
    patch.push(insertOp);
    return [offset, patch];
  }
 
  public createInsertEmbedOp(delta: Delta, pos: number): [offset: number, patch: QuillDeltaOp[]] {
    const length = delta.length();
    const remaining = length - pos;
    const offset = !remaining ? 0 : randomU32(0, remaining);
    const insert = {link: RandomJson.genString(randomU32(1, 5))};
    const patch: QuillDeltaOp[] = [];
    if (offset > 0) {
      patch.push({retain: offset});
    }
    const insertOp: QuillDeltaOpInsert = {insert};
    if (randomU32(0, 1)) {
      const attributes = removeErasures(this.createAttributes());
      if (attributes && !isEmpty(attributes)) {
        insertOp.attributes = attributes;
      }
    }
    return [offset, patch];
  }
 
  public createDeleteOp(length: number, pos: number): [offset: number, patch: QuillDeltaOp[]] {
    const remaining = length - pos;
    Iif (remaining <= 0) return [pos, []];
    const deleteLength = randomU32(1, remaining);
    const patch: QuillDeltaOp[] = [{delete: deleteLength}];
    return [deleteLength, patch];
  }
 
  public createAnnotateOp(length: number, pos: number): [offset: number, patch: QuillDeltaOp[]] {
    const remaining = length - pos;
    Iif (remaining <= 0) return [pos, []];
    const offset = remaining < 2 ? 0 : randomU32(0, remaining - 1);
    const annotationLength = randomU32(1, remaining - offset);
    const patch: QuillDeltaOp[] = [];
    if (offset > 0) {
      patch.push({retain: offset});
    }
    const retainOp: QuillDeltaOpRetain = {retain: annotationLength};
    const attributes = this.createAttributes();
    if (attributes && !isEmpty(attributes)) {
      retainOp.attributes = attributes;
    }
    patch.push(retainOp);
    return [offset + annotationLength, patch];
  }
 
  public createAttributes(): QuillDeltaAttributes {
    const length = randomU32(1, 3);
    const attrKeys = ['bold', 'italic', 'color'];
    const attributes: QuillDeltaAttributes = {};
    for (let i = 0; i < length; i++) {
      const attrKey = Fuzzer.pick(attrKeys);
      switch (attrKey) {
        case 'bold': {
          attributes.bold = Fuzzer.pick([true, null]);
          break;
        }
        case 'italic': {
          attributes.italic = Fuzzer.pick([true, null]);
          break;
        }
        case 'color': {
          attributes.color = Fuzzer.pick(['red', 'blue', null]);
          break;
        }
      }
    }
    return attributes;
  }
}