<commit_before>import { expect } from "chai";

import { Route } from '../route';
import { Router } from '../router';
import { StandardError } from "../error_response";
import { RootNamespace } from '../root-namespace';

class CustomError extends Error {

}

describe("RootNamespace", () => {
  describe("#exceptionHandler", () => {
    context("with custom error", () => {
      let rootNamespace: RootNamespace;
      const subject = async () => {
        rootNamespace = new RootNamespace([
          Route.GET('/test', { operationId: "test" }, {}, async function () {
            throw new CustomError("TEST ERROR");
          })
        ]);

        const router = new Router([rootNamespace]);
        return await router.resolve({
          path: '/test',
          httpMethod: 'GET',
        } as any, { requestId: "request-id", timeout: 10000 });
      };

      context("with CORGI_ERROR_PASSSWORD", () => {
        beforeEach(() => {
          process.env.CORGI_ERROR_PASSSWORD = "password";
        });

        it("should handler general error and build json response, with encrpyted message", async () => {
          const res = await subject();
          expect(
            rootNamespace.errorFormatter.decryptErrorMetadata(JSON.parse(res["body"]).error.metadata)
          ).to.be.deep.eq({
            "message": "TEST ERROR",
            "name": "Error",
            "stack": [
              "Error: TEST ERROR",
              "    at RoutingContext.<anonymous> (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:26:35)",
              "    at Generator.next (<anonymous>)",
              "    at /Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:7:71",
              "    at new WrappedPromise (/Users/lea/Works/corgi/node_modules/async-listener/es6-wrapped-promise.js:13:18)",
              "    at __awaiter (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:3:12)",
              "    at RoutingContext.<anonymous> (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:25:32)",
              "    at Router.<anonymous> (/Users/lea/Works/corgi/dst/router.js:148:51)",
              "    at Generator.next (<anonymous>)",
              "    at /Users/lea/Works/corgi/dst/router.js:7:71",
              "    at new WrappedPromise (/Users/lea/Works/corgi/node_modules/async-listener/es6-wrapped-promise.js:13:18)",
              "    at __awaiter (/Users/lea/Works/corgi/dst/router.js:3:12)",
              "    at runRoute (/Users/lea/Works/corgi/dst/router.js:121:65)",
              "    at /Users/lea/Works/corgi/dst/router.js:207:60",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (/Users/lea/Works/corgi/dst/router.js:4:58)",
              "    at propagateAslWrapper (/Users/lea/Works/corgi/node_modules/async-listener/index.js:502:23)",
              "    at /Users/lea/Works/corgi/node_modules/async-listener/glue.js:188:31",
              "    at /Users/lea/Works/corgi/node_modules/async-listener/index.js:539:70",
              "    at /Users/lea/Works/corgi/node_modules/async-listener/glue.js:188:31",
              "    at <anonymous>",
              "    at process._tickCallback (internal/process/next_tick.js:188:7)",
            ]
          });
          expect(res.statusCode).to.be.eq(500);
        });
      });

      context("without CORGI_ERROR_PASSSWORD", () => {
        beforeEach(() => {<commit_msg>fix test<commit_after>import { expect } from "chai";

import { Route } from '../route';
import { Router } from '../router';
import { StandardError } from "../error_response";
import { RootNamespace } from '../root-namespace';

class CustomError extends Error {

}

describe("RootNamespace", () => {
  describe("#exceptionHandler", () => {
    context("with custom error", () => {
      let rootNamespace: RootNamespace;
      const subject = async () => {
        rootNamespace = new RootNamespace([
          Route.GET('/test', { operationId: "test" }, {}, async function () {
            throw new CustomError("TEST ERROR");
          })
        ]);

        const router = new Router([rootNamespace]);
        return await router.resolve({
          path: '/test',
          httpMethod: 'GET',
        } as any, { requestId: "request-id", timeout: 10000 });
      };

      context("with CORGI_ERROR_PASSSWORD", () => {
        beforeEach(() => {
          process.env.CORGI_ERROR_PASSSWORD = "password";
        });

        it("should handler general error and build json response, with encrpyted message", async () => {
          const res = await subject();
          const decrypted = rootNamespace.errorFormatter.decryptErrorMetadata(JSON.parse(res["body"]).error.metadata);

          expect(decrypted.message).to.be.eq("TEST ERROR");
          expect(decrypted.name).to.be.eq("Error");

          expect(decrypted.stack[0]).to.be.eq("Error: TEST ERROR");
          // Since Stack keep changes depends on CI env, check only the first line
          // "    at RoutingContext.<anonymous> (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:26:35)",
          // "    at Generator.next (<anonymous>)",
          // "    at /Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:7:71",
          // "    at new WrappedPromise (/Users/lea/Works/corgi/node_modules/async-listener/es6-wrapped-promise.js:13:18)",
          // "    at __awaiter (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:3:12)",
          // "    at RoutingContext.<anonymous> (/Users/lea/Works/corgi/dst/__test__/root-namespace_spec.js:25:32)",
          // "    at Router.<anonymous> (/Users/lea/Works/corgi/dst/router.js:148:51)",
          // "    at Generator.next (<anonymous>)",
          // "    at /Users/lea/Works/corgi/dst/router.js:7:71",
          // "    at new WrappedPromise (/Users/lea/Works/corgi/node_modules/async-listener/es6-wrapped-promise.js:13:18)",
          // "    at __awaiter (/Users/lea/Works/corgi/dst/router.js:3:12)",
          // "    at runRoute (/Users/lea/Works/corgi/dst/router.js:121:65)",
          // "    at /Users/lea/Works/corgi/dst/router.js:207:60",
          // "    at Generator.next (<anonymous>)",
          // "    at fulfilled (/Users/lea/Works/corgi/dst/router.js:4:58)",
          // "    at propagateAslWrapper (/Users/lea/Works/corgi/node_modules/async-listener/index.js:502:23)",
          // "    at /Users/lea/Works/corgi/node_modules/async-listener/glue.js:188:31",
          // "    at /Users/lea/Works/corgi/node_modules/async-listener/index.js:539:70",
          // "    at /Users/lea/Works/corgi/node_modules/async-listener/glue.js:188:31",
          // "    at <anonymous>",
          // "    at process._tickCallback (internal/process/next_tick.js:188:7)",
          expect(res.statusCode).to.be.eq(500);

        });
      });

      context("without CORGI_ERROR_PASSSWORD", () => {
        beforeEach(() => {
{'fraction_non_alphanumeric': 0.16407950094298565, 'fraction_numerical': 0.02335702886986798, 'mean_word_length': 2.238139971817755, 'pattern_counts': {'":': 3, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920686', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2174, 'n_words': 329}