|
| 1 | +import { module, test } from "qunit"; |
| 2 | +import DiffStreamer from "discourse/plugins/discourse-ai/discourse/lib/diff-streamer"; |
| 3 | + |
| 4 | +module("Unit | Lib | diff-streamer", function () { |
| 5 | + test("streamingDiff correctly handles trivial cases", async function (assert) { |
| 6 | + const originalText = "helo world"; |
| 7 | + const targetText = "hello world"; |
| 8 | + |
| 9 | + const diffStreamer = new DiffStreamer(this.originalTextContent); |
| 10 | + await diffStreamer.loadJSDiff(); |
| 11 | + |
| 12 | + const diffResult = diffStreamer.streamingDiff(originalText, targetText); |
| 13 | + |
| 14 | + const expectedDiff = [ |
| 15 | + { count: 1, added: false, removed: true, value: "helo" }, |
| 16 | + { count: 1, added: true, removed: false, value: "hello" }, |
| 17 | + { count: 2, added: false, removed: false, value: " world" }, |
| 18 | + ]; |
| 19 | + |
| 20 | + assert.deepEqual( |
| 21 | + diffResult, |
| 22 | + expectedDiff, |
| 23 | + "Diff result should match the expected structure" |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + test("streamingDiff correctly consolidates and handles diff drift", async function (assert) { |
| 28 | + const originalText = |
| 29 | + "This is todone, but I want to can why.\n\nWe\n\nSEO Tags supports a `canonical_url` override. I tried a few possibilities there. The one I wanted to work, ex: `https://www.discourse.org/de`, appended an extra `/de` on the URL, only in the deploy b"; |
| 30 | + const targetText = "This is to-done"; |
| 31 | + |
| 32 | + const diffStreamer = new DiffStreamer(this.originalTextContent); |
| 33 | + await diffStreamer.loadJSDiff(); |
| 34 | + |
| 35 | + const diffResult = diffStreamer.streamingDiff(originalText, targetText); |
| 36 | + |
| 37 | + // Verify the diff result is an array with the expected structure |
| 38 | + assert.true(Array.isArray(diffResult), "Diff result should be an array"); |
| 39 | + assert.strictEqual( |
| 40 | + diffResult.length, |
| 41 | + 3, |
| 42 | + "Expecting exactly three parts in the diff result" |
| 43 | + ); |
| 44 | + |
| 45 | + assert.strictEqual( |
| 46 | + diffResult[0].value, |
| 47 | + "This is ", |
| 48 | + "First part should be unchanged" |
| 49 | + ); |
| 50 | + |
| 51 | + assert.strictEqual( |
| 52 | + diffResult[1].value, |
| 53 | + "to-done", |
| 54 | + "Second part should be an insertion" |
| 55 | + ); |
| 56 | + |
| 57 | + assert.true(diffResult[1].added, "Second part should be an insertion"); |
| 58 | + |
| 59 | + assert.strictEqual( |
| 60 | + diffResult[2].value.length, |
| 61 | + 235, |
| 62 | + "Third part should include all text" |
| 63 | + ); |
| 64 | + |
| 65 | + assert.true(diffResult[2].removed, "Third part should be a removal"); |
| 66 | + }); |
| 67 | +}); |
0 commit comments