|
| 1 | +import child_process from "child_process"; |
| 2 | +import fs from "fs"; |
| 3 | +import { Redis } from "ioredis"; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * @param {number} ms |
| 8 | + * @returns {Promise<void>} |
| 9 | + */ |
| 10 | +const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @param {child_process.ChildProcess} child |
| 15 | + * @returns {Promise<string>} |
| 16 | + */ |
| 17 | +const asPromise = (child) => |
| 18 | + new Promise(function (resolve, reject) { |
| 19 | + child.addListener("error", reject); |
| 20 | + child.addListener("exit", resolve); |
| 21 | + }); |
| 22 | + |
| 23 | +test("run crawl with to-warc-from-raw option and verify text records", async () => { |
| 24 | + fs.rmSync("./test-crawls/raw-text-test", { recursive: true, force: true }); |
| 25 | + |
| 26 | + child_process.execSync( |
| 27 | + "docker run -v $PWD/test-crawls:/crawls webrecorder/browsertrix-crawler crawl --url https://old.webrecorder.net/ --scopeType page --collection raw-text-test --text to-warc,to-warc-from-raw --generateCDX", |
| 28 | + ); |
| 29 | + |
| 30 | + const cdxData = fs.readFileSync( |
| 31 | + "test-crawls/collections/raw-text-test/indexes/index.cdxj", |
| 32 | + { encoding: "utf-8" }, |
| 33 | + ); |
| 34 | + |
| 35 | + // Check that both regular text and raw text records exist |
| 36 | + expect(cdxData.indexOf("urn:text:https://old.webrecorder.net/") >= 0).toBe(true); |
| 37 | + expect(cdxData.indexOf("urn:text-from-response:https://old.webrecorder.net/") >= 0).toBe(true); |
| 38 | +}); |
| 39 | + |
| 40 | +test("verify raw text and rendered text are different for CSR pages", async () => { |
| 41 | + fs.rmSync("./test-crawls/csr-test", { recursive: true, force: true }); |
| 42 | + |
| 43 | + // Crawl a page known to have client-side rendering |
| 44 | + child_process.execSync( |
| 45 | + "docker run -v $PWD/test-crawls:/crawls webrecorder/browsertrix-crawler crawl --url https://old.webrecorder.net/ --scopeType page --collection csr-test --text to-warc,to-warc-from-raw --generateWACZ", |
| 46 | + ); |
| 47 | + |
| 48 | + expect( |
| 49 | + fs.existsSync("test-crawls/collections/csr-test/csr-test.wacz"), |
| 50 | + ).toBe(true); |
| 51 | +}); |
| 52 | + |
| 53 | +test("run QA comparison with raw text match", async () => { |
| 54 | + fs.rmSync("./test-crawls/qa-raw-text-replay", { recursive: true, force: true }); |
| 55 | + |
| 56 | + const child = child_process.exec( |
| 57 | + "docker run -p 36381:6379 -v $PWD/test-crawls:/crawls webrecorder/browsertrix-crawler qa --qaSource /crawls/collections/raw-text-test/raw-text-test.wacz --collection qa-raw-text-replay --crawlId raw-text-test --writePagesToRedis --debugAccessRedis", |
| 58 | + ); |
| 59 | + |
| 60 | + // detect crawler exit |
| 61 | + let crawler_exited = false; |
| 62 | + child.on("exit", function () { |
| 63 | + crawler_exited = true; |
| 64 | + }); |
| 65 | + const crawlerExitedPromise = asPromise(child); |
| 66 | + |
| 67 | + const redis = new Redis("redis://127.0.0.1:36381/0", { |
| 68 | + lazyConnect: true, |
| 69 | + retryStrategy: () => null, |
| 70 | + }); |
| 71 | + |
| 72 | + await sleep(3000); |
| 73 | + |
| 74 | + await redis.connect({ maxRetriesPerRequest: 50 }); |
| 75 | + |
| 76 | + let count = 0; |
| 77 | + |
| 78 | + while (count < 1) { |
| 79 | + const res = await redis.lpop("raw-text-test:pages"); |
| 80 | + if (!res) { |
| 81 | + if (crawler_exited) { |
| 82 | + break; |
| 83 | + } |
| 84 | + await sleep(100); |
| 85 | + continue; |
| 86 | + } |
| 87 | + const json = JSON.parse(res); |
| 88 | + expect(json).toHaveProperty("id"); |
| 89 | + expect(json).toHaveProperty("url"); |
| 90 | + expect(json).toHaveProperty("comparison"); |
| 91 | + |
| 92 | + // Check that raw text comparison is present |
| 93 | + expect(json.comparison).toHaveProperty("rawTextMatch"); |
| 94 | + expect(typeof json.comparison.rawTextMatch).toBe("number"); |
| 95 | + expect(json.comparison.rawTextMatch).toBeGreaterThanOrEqual(0); |
| 96 | + expect(json.comparison.rawTextMatch).toBeLessThanOrEqual(1); |
| 97 | + |
| 98 | + // Check that both regular and raw text comparisons exist |
| 99 | + expect(json.comparison).toHaveProperty("textMatch"); |
| 100 | + expect(json.comparison).toHaveProperty("screenshotMatch"); |
| 101 | + |
| 102 | + count++; |
| 103 | + } |
| 104 | + |
| 105 | + expect(count).toBe(1); |
| 106 | + |
| 107 | + // wait for crawler exit |
| 108 | + await crawlerExitedPromise; |
| 109 | +}); |
| 110 | + |
| 111 | +test("run QA comparison with fallback to on-the-fly raw text extraction", async () => { |
| 112 | + // Use the original qa-wr-net archive which doesn't have stored raw text |
| 113 | + fs.rmSync("./test-crawls/qa-fallback-test", { recursive: true, force: true }); |
| 114 | + |
| 115 | + const child = child_process.exec( |
| 116 | + "docker run -p 36382:6379 -v $PWD/test-crawls:/crawls webrecorder/browsertrix-crawler qa --qaSource /crawls/collections/qa-wr-net/qa-wr-net.wacz --collection qa-fallback-test --crawlId fallback-test --writePagesToRedis --debugAccessRedis --exclude contact", |
| 117 | + ); |
| 118 | + |
| 119 | + // detect crawler exit |
| 120 | + let crawler_exited = false; |
| 121 | + child.on("exit", function () { |
| 122 | + crawler_exited = true; |
| 123 | + }); |
| 124 | + const crawlerExitedPromise = asPromise(child); |
| 125 | + |
| 126 | + const redis = new Redis("redis://127.0.0.1:36382/0", { |
| 127 | + lazyConnect: true, |
| 128 | + retryStrategy: () => null, |
| 129 | + }); |
| 130 | + |
| 131 | + await sleep(3000); |
| 132 | + |
| 133 | + await redis.connect({ maxRetriesPerRequest: 50 }); |
| 134 | + |
| 135 | + let count = 0; |
| 136 | + |
| 137 | + while (count < 3) { |
| 138 | + const res = await redis.lpop("fallback-test:pages"); |
| 139 | + if (!res) { |
| 140 | + if (crawler_exited) { |
| 141 | + break; |
| 142 | + } |
| 143 | + await sleep(100); |
| 144 | + continue; |
| 145 | + } |
| 146 | + const json = JSON.parse(res); |
| 147 | + expect(json).toHaveProperty("id"); |
| 148 | + expect(json).toHaveProperty("url"); |
| 149 | + expect(json).toHaveProperty("comparison"); |
| 150 | + |
| 151 | + // Check that raw text comparison works even without stored raw text |
| 152 | + expect(json.comparison).toHaveProperty("rawTextMatch"); |
| 153 | + expect(typeof json.comparison.rawTextMatch).toBe("number"); |
| 154 | + |
| 155 | + count++; |
| 156 | + } |
| 157 | + |
| 158 | + expect(count).toBe(3); |
| 159 | + |
| 160 | + // wait for crawler exit |
| 161 | + await crawlerExitedPromise; |
| 162 | +}); |
0 commit comments