summaryrefslogtreecommitdiff
path: root/cli/tests/unit/dispatch_json_test.ts
blob: 076c9e6f8a3e8aa56f3cc6ecfdee48e95ce9c664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
  assertStrictEquals,
  unitTest,
  assertMatch,
  unreachable,
} from "./test_util.ts";

const openErrorStackPattern = new RegExp(
  `^.*
    at unwrapResponse \\(.*dispatch_json\\.js:.*\\)
    at sendAsync \\(.*dispatch_json\\.js:.*\\)
    at async Object\\.open \\(.*files\\.js:.*\\).*$`,
  "ms",
);

unitTest(
  { perms: { read: true } },
  async function sendAsyncStackTrace(): Promise<void> {
    await Deno.open("nonexistent.txt")
      .then(unreachable)
      .catch((error): void => {
        assertMatch(error.stack, openErrorStackPattern);
      });
  },
);

declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Deno {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    var core: any; // eslint-disable-line no-var
  }
}

unitTest(function malformedJsonControlBuffer(): void {
  const opId = Deno.core.ops()["op_open_sync"];
  const argsBuf = new Uint8Array([1, 2, 3, 4, 5]);
  const resBuf = Deno.core.send(opId, argsBuf);
  const resText = new TextDecoder().decode(resBuf);
  const resObj = JSON.parse(resText);
  assertStrictEquals(resObj.ok, undefined);
  assertStrictEquals(resObj.err.className, "SyntaxError");
  assertMatch(resObj.err.message, /\bexpected value\b/);
});

unitTest(function invalidPromiseId(): void {
  const opId = Deno.core.ops()["op_open_async"];
  const argsObj = {
    promiseId: "1. NEIN!",
    path: "/tmp/P.I.S.C.I.X/yeah",
    mode: 0o666,
    options: {
      read: true,
      write: true,
      create: true,
      truncate: false,
      append: false,
      createNew: false,
    },
  };
  const argsText = JSON.stringify(argsObj);
  const argsBuf = new TextEncoder().encode(argsText);
  const resBuf = Deno.core.send(opId, argsBuf);
  const resText = new TextDecoder().decode(resBuf);
  const resObj = JSON.parse(resText);
  console.error(resText);
  assertStrictEquals(resObj.ok, undefined);
  assertStrictEquals(resObj.err.className, "TypeError");
  assertMatch(resObj.err.message, /\bpromiseId\b/);
});