tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_webconsole_stubs_evaluation_result.js (5475B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const {
      7  STUBS_UPDATE_ENV,
      8  getCleanedPacket,
      9  getSerializedPacket,
     10  getStubFile,
     11  writeStubsToFile,
     12 } = require(`${CHROME_URL_ROOT}stub-generator-helpers`);
     13 
     14 const TEST_URI = "data:text/html;charset=utf-8,<!DOCTYPE html>stub generation";
     15 const STUB_FILE = "evaluationResult.js";
     16 
     17 add_task(async function () {
     18  const isStubsUpdate = Services.env.get(STUBS_UPDATE_ENV) == "true";
     19  info(`${isStubsUpdate ? "Update" : "Check"} ${STUB_FILE}`);
     20 
     21  const generatedStubs = await generateEvaluationResultStubs();
     22 
     23  if (isStubsUpdate) {
     24    await writeStubsToFile(STUB_FILE, generatedStubs);
     25    ok(true, `${STUB_FILE} was updated`);
     26    return;
     27  }
     28 
     29  const existingStubs = getStubFile(STUB_FILE);
     30  const FAILURE_MSG =
     31    "The evaluationResult stubs file needs to be updated by running `" +
     32    `mach test ${getCurrentTestFilePath()} --headless --setenv WEBCONSOLE_STUBS_UPDATE=true` +
     33    "`";
     34 
     35  if (generatedStubs.size !== existingStubs.rawPackets.size) {
     36    ok(false, FAILURE_MSG);
     37    return;
     38  }
     39 
     40  let failed = false;
     41  for (const [key, packet] of generatedStubs) {
     42    const packetStr = getSerializedPacket(packet, {
     43      sortKeys: true,
     44      replaceActorIds: true,
     45    });
     46    const existingPacketStr = getSerializedPacket(
     47      existingStubs.rawPackets.get(key),
     48      { sortKeys: true, replaceActorIds: true }
     49    );
     50    is(packetStr, existingPacketStr, `"${key}" packet has expected value`);
     51    failed = failed || packetStr !== existingPacketStr;
     52  }
     53 
     54  if (failed) {
     55    ok(false, FAILURE_MSG);
     56  } else {
     57    ok(true, "Stubs are up to date");
     58  }
     59 
     60  await closeTabAndToolbox();
     61 });
     62 
     63 async function generateEvaluationResultStubs() {
     64  const stubs = new Map();
     65  const toolbox = await openNewTabAndToolbox(TEST_URI, "webconsole");
     66  for (const [key, code] of getCommands()) {
     67    const packet = await toolbox.commands.scriptCommand.execute(code);
     68    stubs.set(key, getCleanedPacket(key, packet));
     69  }
     70 
     71  return stubs;
     72 }
     73 
     74 function getCommands() {
     75  const evaluationResultCommands = [
     76    "new Date(0)",
     77    "asdf()",
     78    "1 + @",
     79    "inspect({a: 1})",
     80    "undefined",
     81  ];
     82 
     83  const evaluationResult = new Map(
     84    evaluationResultCommands.map(cmd => [cmd, cmd])
     85  );
     86  evaluationResult.set(
     87    "longString message Error",
     88    `throw new Error("Long error ".repeat(10000))`
     89  );
     90 
     91  evaluationResult.set(`eval throw ""`, `throw ""`);
     92  evaluationResult.set(`eval throw "tomato"`, `throw "tomato"`);
     93  evaluationResult.set(`eval throw false`, `throw false`);
     94  evaluationResult.set(`eval throw 0`, `throw 0`);
     95  evaluationResult.set(`eval throw null`, `throw null`);
     96  evaluationResult.set(`eval throw undefined`, `throw undefined`);
     97  evaluationResult.set(`eval throw Symbol`, `throw Symbol("potato")`);
     98  evaluationResult.set(`eval throw Object`, `throw {vegetable: "cucumber"}`);
     99  evaluationResult.set(`eval throw Error Object`, `throw new Error("pumpkin")`);
    100  evaluationResult.set(
    101    `eval throw Error Object with custom name`,
    102    `
    103    var err = new Error("pineapple");
    104    err.name = "JuicyError";
    105    err.flavor = "delicious";
    106    throw err;
    107  `
    108  );
    109  evaluationResult.set(
    110    `eval throw Error Object with error cause`,
    111    `
    112    var originalError = new SyntaxError("original error")
    113    var err = new Error("something went wrong", {
    114      cause: originalError
    115    });
    116    throw err;
    117  `
    118  );
    119  evaluationResult.set(
    120    `eval throw Error Object with cause chain`,
    121    `
    122    var errA = new Error("err-a")
    123    var errB = new Error("err-b", { cause: errA })
    124    var errC = new Error("err-c", { cause: errB })
    125    var errD = new Error("err-d", { cause: errC })
    126    throw errD;
    127  `
    128  );
    129  evaluationResult.set(
    130    `eval throw Error Object with cyclical cause chain`,
    131    `
    132    var errX = new Error("err-x", { cause: errY})
    133    var errY = new Error("err-y", { cause: errX })
    134    throw errY;
    135  `
    136  );
    137  evaluationResult.set(
    138    `eval throw Error Object with falsy cause`,
    139    `throw new Error("false cause", { cause: false });`
    140  );
    141  evaluationResult.set(
    142    `eval throw Error Object with null cause`,
    143    `throw new Error("null cause", { cause: null });`
    144  );
    145  evaluationResult.set(
    146    `eval throw Error Object with undefined cause`,
    147    `throw new Error("undefined cause", { cause: undefined });`
    148  );
    149  evaluationResult.set(
    150    `eval throw Error Object with number cause`,
    151    `throw new Error("number cause", { cause: 0 });`
    152  );
    153  evaluationResult.set(
    154    `eval throw Error Object with string cause`,
    155    `throw new Error("string cause", { cause: "cause message" });`
    156  );
    157  evaluationResult.set(
    158    `eval throw Error Object with object cause`,
    159    `throw new Error("object cause", { cause: { code: 234, message: "ERR_234"} });`
    160  );
    161 
    162  evaluationResult.set(`eval pending promise`, `new Promise(() => {})`);
    163  evaluationResult.set(`eval Promise.resolve`, `Promise.resolve(123)`);
    164  evaluationResult.set(`eval Promise.reject`, `Promise.reject("ouch")`);
    165  evaluationResult.set(
    166    `eval resolved promise`,
    167    `Promise.resolve().then(() => 246)`
    168  );
    169  evaluationResult.set(
    170    `eval rejected promise`,
    171    `Promise.resolve().then(() => a.b.c)`
    172  );
    173  evaluationResult.set(
    174    `eval rejected promise with Error`,
    175    `Promise.resolve().then(() => {
    176      try {
    177        a.b.c
    178      } catch(e) {
    179        throw new Error("something went wrong", { cause: e })
    180      }
    181    })`
    182  );
    183 
    184  return evaluationResult;
    185 }