quake2ts 0.0.596 → 0.0.598

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,7 @@ __export(index_exports, {
41
41
  captureCanvasDrawCalls: () => captureCanvasDrawCalls,
42
42
  captureGameScreenshot: () => captureGameScreenshot,
43
43
  captureGameState: () => captureGameState,
44
+ compareScreenshot: () => compareScreenshot,
44
45
  compareScreenshots: () => compareScreenshots,
45
46
  createBandwidthTestScenario: () => createBandwidthTestScenario,
46
47
  createBinaryStreamMock: () => createBinaryStreamMock,
@@ -165,6 +166,7 @@ __export(index_exports, {
165
166
  simulateServerTick: () => simulateServerTick,
166
167
  simulateSnapshotDelivery: () => simulateSnapshotDelivery,
167
168
  stairTrace: () => import_shared2.stairTrace,
169
+ takeScreenshot: () => takeScreenshot,
168
170
  teardownBrowserEnvironment: () => teardownBrowserEnvironment,
169
171
  teardownMockAudioContext: () => teardownMockAudioContext,
170
172
  teardownNodeEnvironment: () => teardownNodeEnvironment,
@@ -3236,9 +3238,81 @@ function throttleBandwidth(bytesPerSecond) {
3236
3238
  }
3237
3239
 
3238
3240
  // src/e2e/visual.ts
3241
+ var import_canvas3 = require("@napi-rs/canvas");
3242
+ var import_promises = __toESM(require("fs/promises"), 1);
3243
+ var import_path = __toESM(require("path"), 1);
3239
3244
  async function captureGameScreenshot(page, name) {
3240
3245
  return await page.screenshot({ path: `${name}.png` });
3241
3246
  }
3247
+ async function takeScreenshot(canvas, filepath) {
3248
+ let buffer;
3249
+ if ("toBuffer" in canvas && typeof canvas.toBuffer === "function") {
3250
+ buffer = canvas.toBuffer("image/png");
3251
+ } else if ("toDataURL" in canvas) {
3252
+ const dataUrl = canvas.toDataURL("image/png");
3253
+ const base64 = dataUrl.replace(/^data:image\/png;base64,/, "");
3254
+ buffer = Buffer.from(base64, "base64");
3255
+ } else {
3256
+ throw new Error("Unsupported canvas type for screenshot");
3257
+ }
3258
+ await import_promises.default.mkdir(import_path.default.dirname(filepath), { recursive: true });
3259
+ await import_promises.default.writeFile(filepath, buffer);
3260
+ }
3261
+ async function compareScreenshot(canvas, baselinePath) {
3262
+ try {
3263
+ await import_promises.default.access(baselinePath);
3264
+ } catch {
3265
+ console.warn(`Baseline not found at ${baselinePath}, saving current as baseline.`);
3266
+ await takeScreenshot(canvas, baselinePath);
3267
+ return true;
3268
+ }
3269
+ const baselineBuffer = await import_promises.default.readFile(baselinePath);
3270
+ const baselineImage = new import_canvas3.Image();
3271
+ baselineImage.src = baselineBuffer;
3272
+ const width = baselineImage.width;
3273
+ const height = baselineImage.height;
3274
+ let currentBuffer;
3275
+ if ("toBuffer" in canvas && typeof canvas.toBuffer === "function") {
3276
+ currentBuffer = canvas.toBuffer("image/png");
3277
+ } else if ("toDataURL" in canvas) {
3278
+ const dataUrl = canvas.toDataURL("image/png");
3279
+ currentBuffer = Buffer.from(dataUrl.replace(/^data:image\/png;base64,/, ""), "base64");
3280
+ } else {
3281
+ throw new Error("Unsupported canvas type");
3282
+ }
3283
+ if (baselineBuffer.equals(currentBuffer)) {
3284
+ return true;
3285
+ }
3286
+ const baselineCanvas = new import_canvas3.Canvas(width, height);
3287
+ const ctx = baselineCanvas.getContext("2d");
3288
+ ctx.drawImage(baselineImage, 0, 0);
3289
+ const baselineData = ctx.getImageData(0, 0, width, height).data;
3290
+ const currentImage = new import_canvas3.Image();
3291
+ currentImage.src = currentBuffer;
3292
+ if (currentImage.width !== width || currentImage.height !== height) {
3293
+ console.error(`Dimension mismatch: Baseline ${width}x${height} vs Current ${currentImage.width}x${currentImage.height}`);
3294
+ return false;
3295
+ }
3296
+ const currentCanvas = new import_canvas3.Canvas(width, height);
3297
+ const ctx2 = currentCanvas.getContext("2d");
3298
+ ctx2.drawImage(currentImage, 0, 0);
3299
+ const currentData = ctx2.getImageData(0, 0, width, height).data;
3300
+ let diffPixels = 0;
3301
+ const totalPixels = width * height;
3302
+ for (let i = 0; i < baselineData.length; i += 4) {
3303
+ if (baselineData[i] !== currentData[i] || // R
3304
+ baselineData[i + 1] !== currentData[i + 1] || // G
3305
+ baselineData[i + 2] !== currentData[i + 2] || // B
3306
+ baselineData[i + 3] !== currentData[i + 3]) {
3307
+ diffPixels++;
3308
+ }
3309
+ }
3310
+ if (diffPixels > 0) {
3311
+ console.error(`Visual regression: ${diffPixels} pixels differ (${(diffPixels / totalPixels * 100).toFixed(2)}%)`);
3312
+ return false;
3313
+ }
3314
+ return true;
3315
+ }
3242
3316
  function compareScreenshots(baseline, current, threshold = 0.01) {
3243
3317
  if (baseline.length !== current.length) {
3244
3318
  return { diffPercentage: 1 };
@@ -3253,7 +3327,6 @@ function compareScreenshots(baseline, current, threshold = 0.01) {
3253
3327
  const diffPercentage = diffPixels / totalPixels;
3254
3328
  return {
3255
3329
  diffPercentage
3256
- // Generating a diff image buffer would require a library like pixelmatch
3257
3330
  };
3258
3331
  }
3259
3332
  function createVisualTestScenario(sceneName) {
@@ -3276,6 +3349,7 @@ function createVisualTestScenario(sceneName) {
3276
3349
  captureCanvasDrawCalls,
3277
3350
  captureGameScreenshot,
3278
3351
  captureGameState,
3352
+ compareScreenshot,
3279
3353
  compareScreenshots,
3280
3354
  createBandwidthTestScenario,
3281
3355
  createBinaryStreamMock,
@@ -3400,6 +3474,7 @@ function createVisualTestScenario(sceneName) {
3400
3474
  simulateServerTick,
3401
3475
  simulateSnapshotDelivery,
3402
3476
  stairTrace,
3477
+ takeScreenshot,
3403
3478
  teardownBrowserEnvironment,
3404
3479
  teardownMockAudioContext,
3405
3480
  teardownNodeEnvironment,