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 26767x 26767x 5450x   2785x 2785x 2785x     2665x 2665x 2665x     5450x 5450x   21317x   5320x 5320x 5320x     5359x 5359x 5359x     5280x 5280x 5280x     5358x 5358x 5358x     21317x   15000x                 8105x 8105x 8105x 8105x 8105x 8105x 4110x   8105x 8105x 4113x 4113x 2795x     8105x 8105x       8024x 8024x 8024x 8024x 8024x 8024x 4239x   8024x 8024x 3934x 3934x 2749x     8024x       5280x 5280x 5280x 5280x 5280x       5358x 5358x 5358x 5358x 5358x 5358x 3551x   5358x 5358x 5358x 5358x   5358x 5358x       13405x 13405x 13405x 13405x 26664x 26664x   9024x 9024x     8789x 8789x     8851x 8851x       13405x      
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;
  }
}