Migrate from Ava test framework to node:test

This commit is contained in:
Zaid Humaimidi committed 2023-12-17 22:26:52 -08:00
1 parent 0c9c027a77
commit ddb240d542
12 files changed
+136 -4562

No files matched your search

-4434
View File
File diff suppressed because it is too large. Load diff
+1 -2
View File
@@ -47,7 +47,6 @@
"@types/seedrandom": "^3.0.8",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"ava": "^3.15.0",
"codemirror": "^5.65.16",
"esbuild": "^0.19.9",
"eslint": "^8.56.0",
@@ -63,7 +62,7 @@
"node": ">=12.13.0"
},
"scripts": {
"test": "esbuild --log-level=warning --bundle --platform=node --target=node12 --packages=external --outdir=test test/*.ts && ava --verbose --color && rm test/*.js",
"test": "esbuild --log-level=warning --bundle --platform=node --target=node18 --packages=external --outdir=test test/*.ts && node --test test/*.js && rm test/*.js",
"lint": "esbuild build/lint.ts --bundle --platform=node --target=node16 --packages=external | node",
"build": "esbuild build/build.ts --bundle --platform=node --target=node12 --packages=external | node"
}
+4 -5
View File
@@ -1,8 +1,9 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import { randomBytes } from "crypto";
import * as auth from "../lib/auth";
ava("digest", (t) => {
void test("digest", () => {
const username = "test";
const password = "test";
const uri = "/";
@@ -17,8 +18,6 @@ ava("digest", (t) => {
`Digest realm="${realm}",nonce="${nonce}",qop="auth-int"`,
];
t.plan(challenges.length);
for (const challenge of challenges) {
const wwwAuthHeader = auth.parseWwwAuthenticateHeader(challenge);
const solution = auth.solveDigest(
@@ -30,7 +29,7 @@ ava("digest", (t) => {
wwwAuthHeader,
);
const authHeader = auth.parseAuthorizationHeader(solution);
t.is(
assert.strictEqual(
authHeader["response"],
auth.digest(
username,
+14 -15
View File
@@ -1,11 +1,12 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import { EJSON } from "bson";
import { Filter } from "mongodb";
import { stringify, parse } from "../lib/common/expression/parser";
import { convertOldPrecondition } from "../lib/db/util";
import { toMongoQuery } from "../lib/db/synth";
ava("convertOldPrecondition", (t) => {
void test("convertOldPrecondition", () => {
const tests = [
[{}, "true"],
[{ test: "test" }, 'test = "test"'],
@@ -49,26 +50,24 @@ ava("convertOldPrecondition", (t) => {
[{ _tags: [] }, "Invalid type"],
[{ _tags: { $gt: "test" } }, "Invalid tag query"],
[{ $nor: [] }, "Operator $nor not supported"],
];
] as [Filter<unknown>, string][];
t.plan(tests.length + 2 * shouldFailTests.length);
for (const test of tests) {
t.is(
stringify(convertOldPrecondition(test[0] as Record<string, unknown>)),
test[1],
for (const t of tests) {
assert.strictEqual(
stringify(convertOldPrecondition(t[0] as Record<string, unknown>)),
t[1],
);
}
for (const test of shouldFailTests) {
for (const t of shouldFailTests) {
const func = (): void => {
convertOldPrecondition(test[0] as Record<string, unknown>);
convertOldPrecondition(t[0]);
};
const error = t.throws(func, { instanceOf: Error });
t.is(error.message, test[1]);
assert.throws(func, new Error(t[1]));
}
});
ava("toMongoQuery", async (t) => {
void test("toMongoQuery", async () => {
const queries: [string, Filter<unknown> | false][] = [
["true", {}],
["Tags.tag1 = true", { _tags: { $eq: "tag1" } }],
@@ -150,7 +149,7 @@ ava("toMongoQuery", async (t) => {
const exp = parse(expStr);
let query = toMongoQuery(exp, "devices");
if (query) query = EJSON.serialize(query);
t.deepEqual(query, expect);
assert.deepStrictEqual(query, expect);
}
const failQueries: [any, string][] = [
@@ -162,7 +161,7 @@ ava("toMongoQuery", async (t) => {
for (const [expStr, err] of failQueries) {
const exp = parse(expStr);
t.throws(() => toMongoQuery(exp, "devices"), {
assert.throws(() => toMongoQuery(exp, "devices"), {
message: err,
});
}
+15 -15
View File
@@ -1,28 +1,28 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import Path from "../lib/common/path";
import * as device from "../lib/device";
import PathSet from "../lib/common/path-set";
import VersionedMap from "../lib/versioned-map";
import { Attributes } from "../lib/types";
ava("getAliasDeclarations", (t) => {
void test("getAliasDeclarations", () => {
const path = Path.parse("a.[aa:10,bb.[aaa:100].cc:1].b");
const decs = device.getAliasDeclarations(path, 99);
const expected = ["a.*.b", "a.*.aa", "a.*.bb.*.cc", "a.*.bb.*.aaa"];
t.plan(24);
for (const [i, d] of decs.entries()) {
t.is(d.path.toString(), expected[i]);
t.is(d.pathGet, 99);
t.is(d.pathSet, null);
t.deepEqual(d.attrGet, i ? { value: 99 } : null);
t.is(d.attrSet, null);
t.is(d.defer, true);
assert.strictEqual(d.path.toString(), expected[i]);
assert.strictEqual(d.pathGet, 99);
assert.strictEqual(d.pathSet, null);
assert.deepStrictEqual(d.attrGet, i ? { value: 99 } : null);
assert.strictEqual(d.attrSet, null);
assert.strictEqual(d.defer, true);
}
});
ava("unpack", (t) => {
void test("unpack", () => {
const now = Date.now();
const deviceData = {
paths: new PathSet(),
@@ -87,11 +87,11 @@ ava("unpack", (t) => {
deviceData,
Path.parse("a.[b:b,c:c].a.[b:b1,c:c1].a"),
);
t.is(unpacked.length, 2);
t.is(unpacked[0].toString(), "a.1.a.1.a");
t.is(unpacked[1].toString(), "a.2.a.1.a");
assert.strictEqual(unpacked.length, 2);
assert.strictEqual(unpacked[0].toString(), "a.1.a.1.a");
assert.strictEqual(unpacked[1].toString(), "a.2.a.1.a");
unpacked = device.unpack(deviceData, Path.parse("a.*.a.[b:c1,c:b1].a"));
t.is(unpacked.length, 1);
t.is(unpacked[0].toString(), "a.2.a.2.a");
assert.strictEqual(unpacked.length, 1);
assert.strictEqual(unpacked[0].toString(), "a.2.a.2.a");
});
+18 -17
View File
@@ -1,34 +1,35 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import Path from "../lib/common/path";
import PathSet from "../lib/common/path-set";
ava("depth", (t) => {
void test("depth", () => {
const pathSet = new PathSet();
t.is(pathSet.depth, 0);
assert.strictEqual(pathSet.depth, 0);
pathSet.add(Path.parse(""));
t.is(pathSet.depth, 1);
assert.strictEqual(pathSet.depth, 1);
pathSet.add(Path.parse("a"));
t.is(pathSet.depth, 2);
assert.strictEqual(pathSet.depth, 2);
});
ava("add", (t) => {
void test("add", () => {
const pathSet = new PathSet();
pathSet.add(Path.parse("a"));
pathSet.add(Path.parse("a"));
t.is(pathSet.find(Path.parse("a"), true, true, 99).length, 1);
assert.strictEqual(pathSet.find(Path.parse("a"), true, true, 99).length, 1);
});
ava("get", (t) => {
void test("get", () => {
const pathSet = new PathSet();
pathSet.add(Path.parse("a.*"));
pathSet.add(Path.parse("a.a"));
pathSet.add(Path.parse("*.*"));
t.is(pathSet.get(Path.parse("a.*")).toString(), "a.*");
t.is(pathSet.get(Path.parse("*.a")), null);
assert.strictEqual(pathSet.get(Path.parse("a.*")).toString(), "a.*");
assert.strictEqual(pathSet.get(Path.parse("*.a")), null);
});
ava("find", (t) => {
void test("find", () => {
const pathSet = new PathSet();
pathSet.add(Path.parse("a"));
pathSet.add(Path.parse("a.*"));
@@ -36,32 +37,32 @@ ava("find", (t) => {
pathSet.add(Path.parse("*.a"));
pathSet.add(Path.parse("*.*"));
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse(""), true, true, 1).map((p) => p.toString()),
["a"],
);
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse(""), false, false, 2).map((p) => p.toString()),
["a", "a.*", "a.a", "*.a", "*.*"],
);
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse("a.*"), false, false).map((p) => p.toString()),
["a.*"],
);
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse("a.*"), false, true).map((p) => p.toString()),
["a.*", "a.a"],
);
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse("a.*"), true, false).map((p) => p.toString()),
["a.*", "*.*"],
);
t.deepEqual(
assert.deepStrictEqual(
pathSet.find(Path.parse("a.*"), true, true).map((p) => p.toString()),
["a.*", "a.a", "*.a", "*.*"],
);
+29 -28
View File
@@ -1,44 +1,45 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import Path from "../lib/common/path";
ava("parse", (t) => {
t.throws(() => Path.parse("."));
t.throws(() => Path.parse("a "));
t.throws(() => Path.parse(".a"));
t.throws(() => Path.parse("a."));
t.throws(() => Path.parse("a..b"));
t.throws(() => Path.parse("b*"));
t.throws(() => Path.parse("*b"));
t.throws(() => Path.parse("a.b c.d"));
t.throws(() => Path.parse("a["));
t.throws(() => Path.parse("a[b"));
t.throws(() => Path.parse("a[b:"));
t.throws(() => Path.parse('a[b:"waef]'));
t.notThrows(() => Path.parse("[]"));
t.notThrows(() => Path.parse("[a:]"));
t.notThrows(() => Path.parse("*"));
t.notThrows(() => Path.parse(""));
void test("parse", () => {
assert.throws(() => Path.parse("."));
assert.throws(() => Path.parse("a "));
assert.throws(() => Path.parse(".a"));
assert.throws(() => Path.parse("a."));
assert.throws(() => Path.parse("a..b"));
assert.throws(() => Path.parse("b*"));
assert.throws(() => Path.parse("*b"));
assert.throws(() => Path.parse("a.b c.d"));
assert.throws(() => Path.parse("a["));
assert.throws(() => Path.parse("a[b"));
assert.throws(() => Path.parse("a[b:"));
assert.throws(() => Path.parse('a[b:"waef]'));
assert.doesNotThrow(() => Path.parse("[]"));
assert.doesNotThrow(() => Path.parse("[a:]"));
assert.doesNotThrow(() => Path.parse("*"));
assert.doesNotThrow(() => Path.parse(""));
});
ava("toString", (t) => {
void test("toString", () => {
const path1 = Path.parse('abc.[ abc : 123 , 123 : abc , 123: " abc "].123');
const path2 = Path.parse('abc.[123: " abc ",abc:123,123:abc].123');
t.is(path1.toString(), path2.toString());
assert.strictEqual(path1.toString(), path2.toString());
});
ava("slice", (t) => {
void test("slice", () => {
const path = Path.parse("a.*.b.[x:y].c");
const sliced = path.slice(1, -1);
t.is(sliced.toString(), '*.b.[x:"y"]');
t.is(sliced.alias, 0b100);
t.is(sliced.wildcard, 0b1);
assert.strictEqual(sliced.toString(), '*.b.[x:"y"]');
assert.strictEqual(sliced.alias, 0b100);
assert.strictEqual(sliced.wildcard, 0b1);
});
ava("concat", (t) => {
void test("concat", () => {
const path1 = Path.parse("a");
const path2 = Path.parse("*.[a:b]");
const concat = path1.concat(path2);
t.is(concat.toString(), 'a.*.[a:"b"]');
t.is(concat.alias, 0b100);
t.is(concat.wildcard, 0b10);
assert.strictEqual(concat.toString(), 'a.*.[a:"b"]');
assert.strictEqual(concat.alias, 0b100);
assert.strictEqual(concat.wildcard, 0b10);
});
+6 -5
View File
@@ -1,7 +1,8 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import { parsePing } from "../lib/ping";
ava("linux Case-1", (t) => {
void test("linux Case-1", () => {
const platform = "linux";
const stdout =
"PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data\n64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=6.66 ms\n64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=6.50 ms\n64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=6.38 ms\n\n--- 8.8.8.8 ping statistics ---\n3 packets transmitted, 3 received, 0% packet loss, time 402ms\nrtt min/avg/max/mdev = 6.381/6.511/6.656/0.112 ms";
@@ -15,10 +16,10 @@ ava("linux Case-1", (t) => {
mdev: 0.112,
};
const parsed = parsePing(platform, stdout);
t.deepEqual(parsedResult, parsed);
assert.deepStrictEqual(parsedResult, parsed);
});
ava("linux Case-2", (t) => {
void test("linux Case-2", () => {
const platform = "linux";
const stdout =
"PING 10.251.9.108 (10.251.9.108): 56 data bytes\n64 bytes from 10.251.9.108: icmp_seq=0 ttl=57 time=36.758 ms\n64 bytes from 10.251.9.108: icmp_seq=1 ttl=57 time=69.094 ms\n64 bytes from 10.251.9.108: icmp_seq=2 ttl=57 time=28.868 ms\n--- 10.251.9.108 ping statistics ---3 packets transmitted, 3 packets received, 0% packet loss\nround-trip min/avg/max/stddev = 28.868/44.907/69.094/17.404 ms";
@@ -32,5 +33,5 @@ ava("linux Case-2", (t) => {
mdev: 17.404,
};
const parsed = parsePing(platform, stdout);
t.deepEqual(parsedResult, parsed);
assert.deepStrictEqual(parsedResult, parsed);
});
+24 -25
View File
@@ -1,24 +1,18 @@
import ava, { TestInterface } from "ava";
import test from "node:test";
import assert from "node:assert";
import { minimize, unionDiff } from "../lib/common/expression/synth";
import { parse, stringify } from "../lib/common/expression/parser";
import initSqlJs from "sql.js/dist/sql-asm.js";
const test = ava as TestInterface<{
query: (filter: string) => Promise<Set<number>>;
}>;
const STRING_VALUES = [null, "", "a", "ab", "ab10", "ab-10"];
const DECIMAL_VALUES = [null, 0, -10, 10];
test.before(async (t) => {
const sql = await initSqlJs();
const db = new sql.Database();
let db;
t.context.query = async (filter: string): Promise<Set<number>> => {
const res = db.exec(`SELECT id FROM test WHERE ${filter}`);
if (!res.length) return new Set();
return new Set(res[0].values.flat());
};
async function query(filter: string): Promise<Set<number>> {
if (!db) {
const sql = await initSqlJs();
db = new sql.Database();
db.run(
"CREATE TABLE test (id INTEGER PRIMARY KEY, string STRING, decimal DECIMAL(4,2))",
@@ -32,7 +26,12 @@ test.before(async (t) => {
stmt.run([STRING_VALUES[str], DECIMAL_VALUES[dec]]);
}
stmt.free();
});
}
const res = db.exec(`SELECT id FROM test WHERE ${filter}`);
if (!res.length) return new Set();
return new Set(res[0].values.flat());
}
function setsEqual(set1: Set<number>, set2: Set<number>): boolean {
if (set1.size !== set2.size) return false;
@@ -56,7 +55,7 @@ function getPermutations(...arrs: any[][]): any[][] {
return res;
}
test("minimize", async (t) => {
void test("minimize", async () => {
const cases: string[] = [];
cases.push("null");
@@ -86,14 +85,14 @@ test("minimize", async (t) => {
cases.push(`string > "${s1}" AND string < '${s2}'`);
for (const c of cases) {
const res1 = await t.context.query(c);
const res1 = await query(c);
const min = stringify(minimize(parse(c), true));
const res2 = await t.context.query(min);
t.true(setsEqual(res1, res2));
const res2 = await query(min);
assert.strictEqual(setsEqual(res1, res2), true);
}
});
test("unionDiff", async (t) => {
void test("unionDiff", async () => {
const cases = [
"true",
"decimal > 0",
@@ -103,16 +102,16 @@ test("unionDiff", async (t) => {
];
for (const [c1, c2] of getPermutations(cases, cases)) {
const res1 = await t.context.query(c1);
const res2 = await t.context.query(c2);
const res1 = await query(c1);
const res2 = await query(c2);
const [union, diff] = unionDiff(parse(c1), parse(c2));
const res3 = await t.context.query(stringify(union));
const res4 = await t.context.query(stringify(diff));
const res3 = await query(stringify(union));
const res4 = await query(stringify(diff));
const unionSet = new Set([...res1, ...res2]);
const diffSet = new Set(Array.from(res2).filter((r) => !res1.has(r)));
t.true(setsEqual(res3, unionSet));
t.true(setsEqual(res4, diffSet));
assert.strictEqual(setsEqual(res3, unionSet), true);
assert.strictEqual(setsEqual(res4, diffSet), true);
}
});
+10 -6
View File
@@ -1,7 +1,8 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import * as common from "../lib/util";
ava("generateDeviceId", (t) => {
void test("generateDeviceId", () => {
const space = [" ", "%20"];
const special = [";", "%3B"];
const cases = [
@@ -28,13 +29,16 @@ ava("generateDeviceId", (t) => {
`TestOUIWith${space[1]}_${special[1]}2912-TestSerialNumberWith${space[1]}_${special[1]}2912`,
],
];
t.plan(cases.length);
for (const c of cases)
t.is(common.generateDeviceId(c[0] as Record<string, string>), c[1]);
assert.strictEqual(
common.generateDeviceId(c[0] as Record<string, string>),
c[1],
);
});
ava("escapeRegExp", (t) => {
t.is(
void test("escapeRegExp", () => {
assert.strictEqual(
common.escapeRegExp("\\ ^ $ * + ? . ( ) | { } [ ]"),
"\\\\ \\^ \\$ \\* \\+ \\? \\. \\( \\) \\| \\{ \\} \\[ \\]",
);
+8 -7
View File
@@ -1,16 +1,17 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import {
parseXmlDeclaration,
decodeEntities,
parseXml,
} from "../lib/xml-parser";
ava("parseXmlDeclaration", (t) => {
void test("parseXmlDeclaration", () => {
const buf = Buffer.from(
'<?xml version="1.0" encoding="UTF-8"?>\n<soap-env:Envelope />',
);
const attrs = parseXmlDeclaration(buf);
t.deepEqual(attrs, [
assert.deepStrictEqual(attrs, [
{
name: "version",
namespace: "",
@@ -26,18 +27,18 @@ ava("parseXmlDeclaration", (t) => {
]);
});
ava("decodeEntities", (t) => {
t.is(
void test("decodeEntities", () => {
assert.strictEqual(
decodeEntities("&&amp;&lt;&gt;&quot;&apos;&gt;&#167;&#xd842;&#xDFB7;;"),
"&&<>\"'>§𠮷;",
);
});
ava("parse", (t) => {
void test("parse", () => {
const xml =
'<?xml version="1.0"?>\n<a-b:c><d f="1<g>"/><!-- comment --><h >i</h></a-b:c>';
const parsed = parseXml(xml);
t.deepEqual(parsed, {
assert.deepStrictEqual(parsed, {
name: "root",
namespace: "",
localName: "root",
+7 -3
View File
@@ -1,14 +1,18 @@
import ava from "ava";
import test from "node:test";
import assert from "node:assert";
import * as yaml from "yaml";
import { stringify } from "../lib/common/yaml";
import testCases from "./yaml-tests.json";
ava("stringify", (t) => {
void test("stringify", () => {
for (const testCase of testCases) {
let str = stringify(testCase);
if (str.startsWith(">2")) str = ">3" + str.slice(2);
if (str.startsWith("|2")) str = "|3" + str.slice(2);
t.deepEqual(yaml.parse(yaml.stringify(testCase)), yaml.parse(str));
assert.deepStrictEqual(
yaml.parse(yaml.stringify(testCase)),
yaml.parse(str),
);
}
});