mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-14 16:15:18 +00:00
Use describe for test grouping, more functions, less const arrows
This commit is contained in:
+127
-125
@@ -1,142 +1,144 @@
|
||||
import { test } from "node:test";
|
||||
import { describe, test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { DateBuilder } from "./DateBuilder";
|
||||
|
||||
test("DateBuilder should be able to be initialised", () => {
|
||||
const db = new DateBuilder();
|
||||
assert.equal(db instanceof DateBuilder, true);
|
||||
});
|
||||
describe("DateBuilder", () => {
|
||||
test("should be able to be initialised", () => {
|
||||
const db = new DateBuilder();
|
||||
assert.equal(db instanceof DateBuilder, true);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to build current date", () => {
|
||||
const now = new Date();
|
||||
const db = new DateBuilder();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), now.getFullYear());
|
||||
assert.equal(built.getMonth(), now.getMonth());
|
||||
assert.equal(built.getDate(), now.getDate());
|
||||
assert.equal(built.getHours(), now.getHours());
|
||||
assert.equal(built.getMinutes(), now.getMinutes());
|
||||
assert.equal(built.getSeconds(), now.getSeconds());
|
||||
});
|
||||
test("should be able to build current date", () => {
|
||||
const now = new Date();
|
||||
const db = new DateBuilder();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), now.getFullYear());
|
||||
assert.equal(built.getMonth(), now.getMonth());
|
||||
assert.equal(built.getDate(), now.getDate());
|
||||
assert.equal(built.getHours(), now.getHours());
|
||||
assert.equal(built.getMinutes(), now.getMinutes());
|
||||
assert.equal(built.getSeconds(), now.getSeconds());
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to build timestamp", () => {
|
||||
const now = new Date();
|
||||
const db = new DateBuilder();
|
||||
const built = db.buildTimestamp();
|
||||
assert.equal(built, now.getTime());
|
||||
});
|
||||
test("should be able to build timestamp", () => {
|
||||
const now = new Date();
|
||||
const db = new DateBuilder();
|
||||
const built = db.buildTimestamp();
|
||||
assert.equal(built, now.getTime());
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to add days", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
|
||||
db.addDays(30);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 0); // January
|
||||
assert.equal(built.getDate(), 31); // January has 31 days
|
||||
});
|
||||
test("should be able to add days", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
|
||||
db.addDays(30);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 0); // January
|
||||
assert.equal(built.getDate(), 31); // January has 31 days
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to add months", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 31, 2024
|
||||
db.addMonths(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
});
|
||||
test("should be able to add months", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 31, 2024
|
||||
db.addMonths(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to add years", () => {
|
||||
const db = new DateBuilder(new Date(2020, 1, 1));
|
||||
db.addYears(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2021);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
assert.equal(built.getDate(), 1);
|
||||
});
|
||||
test("should be able to add years", () => {
|
||||
const db = new DateBuilder(new Date(2020, 1, 1));
|
||||
db.addYears(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2021);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
assert.equal(built.getDate(), 1);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to set date", () => {
|
||||
const db = new DateBuilder();
|
||||
db.withDate(2022, 12, 25); // Dec 25, 2022
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2022);
|
||||
assert.equal(built.getMonth(), 11); // December
|
||||
assert.equal(built.getDate(), 25);
|
||||
});
|
||||
test("should be able to set date", () => {
|
||||
const db = new DateBuilder();
|
||||
db.withDate(2022, 12, 25); // Dec 25, 2022
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2022);
|
||||
assert.equal(built.getMonth(), 11); // December
|
||||
assert.equal(built.getDate(), 25);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to set time", () => {
|
||||
const db = new DateBuilder();
|
||||
db.withTime(15, 30, 45, 123); // 15:30:45.123
|
||||
const built = db.build();
|
||||
assert.equal(built.getHours(), 15);
|
||||
assert.equal(built.getMinutes(), 30);
|
||||
assert.equal(built.getSeconds(), 45);
|
||||
assert.equal(built.getMilliseconds(), 123);
|
||||
});
|
||||
test("should be able to set time", () => {
|
||||
const db = new DateBuilder();
|
||||
db.withTime(15, 30, 45, 123); // 15:30:45.123
|
||||
const built = db.build();
|
||||
assert.equal(built.getHours(), 15);
|
||||
assert.equal(built.getMinutes(), 30);
|
||||
assert.equal(built.getSeconds(), 45);
|
||||
assert.equal(built.getMilliseconds(), 123);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to set start of day", () => {
|
||||
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
|
||||
db.atStartOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 5); // June
|
||||
assert.equal(built.getDate(), 15);
|
||||
assert.equal(built.getHours(), 0);
|
||||
assert.equal(built.getMinutes(), 0);
|
||||
assert.equal(built.getSeconds(), 0);
|
||||
assert.equal(built.getMilliseconds(), 0);
|
||||
});
|
||||
test("should be able to set start of day", () => {
|
||||
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
|
||||
db.atStartOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 5); // June
|
||||
assert.equal(built.getDate(), 15);
|
||||
assert.equal(built.getHours(), 0);
|
||||
assert.equal(built.getMinutes(), 0);
|
||||
assert.equal(built.getSeconds(), 0);
|
||||
assert.equal(built.getMilliseconds(), 0);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to set end of day", () => {
|
||||
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
|
||||
db.atEndOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 5); // June
|
||||
assert.equal(built.getDate(), 15);
|
||||
assert.equal(built.getHours(), 23);
|
||||
assert.equal(built.getMinutes(), 59);
|
||||
assert.equal(built.getSeconds(), 59);
|
||||
assert.equal(built.getMilliseconds(), 999);
|
||||
});
|
||||
test("should be able to set end of day", () => {
|
||||
const db = new DateBuilder(new Date(2024, 5, 15, 10, 20, 30, 456)); // June 15, 2024, 10:20:30.456
|
||||
db.atEndOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 5); // June
|
||||
assert.equal(built.getDate(), 15);
|
||||
assert.equal(built.getHours(), 23);
|
||||
assert.equal(built.getMinutes(), 59);
|
||||
assert.equal(built.getSeconds(), 59);
|
||||
assert.equal(built.getMilliseconds(), 999);
|
||||
});
|
||||
|
||||
test("DateBuilder should be able to chain methods", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
|
||||
db.addDays(1).addMonths(1).addYears(1).withTime(12, 0, 0).atEndOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2025);
|
||||
assert.equal(built.getMonth(), 1); // March
|
||||
assert.equal(built.getDate(), 2);
|
||||
assert.equal(built.getHours(), 23);
|
||||
assert.equal(built.getMinutes(), 59);
|
||||
assert.equal(built.getSeconds(), 59);
|
||||
assert.equal(built.getMilliseconds(), 999);
|
||||
});
|
||||
test("should be able to chain methods", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 1)); // Jan 1, 2024
|
||||
db.addDays(1).addMonths(1).addYears(1).withTime(12, 0, 0).atEndOfDay();
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2025);
|
||||
assert.equal(built.getMonth(), 1); // March
|
||||
assert.equal(built.getDate(), 2);
|
||||
assert.equal(built.getHours(), 23);
|
||||
assert.equal(built.getMinutes(), 59);
|
||||
assert.equal(built.getSeconds(), 59);
|
||||
assert.equal(built.getMilliseconds(), 999);
|
||||
});
|
||||
|
||||
test("DateBuilder should not mutate original date", () => {
|
||||
const original = new Date(2024, 0, 1); // Jan 1, 2024
|
||||
const db = new DateBuilder(original);
|
||||
db.addDays(10);
|
||||
const built = db.build();
|
||||
assert.equal(original.getFullYear(), 2024);
|
||||
assert.equal(original.getMonth(), 0); // January
|
||||
assert.equal(original.getDate(), 1); // Original date should remain unchanged
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 0); // January
|
||||
assert.equal(built.getDate(), 11); // New date should be Jan 11, 2024
|
||||
});
|
||||
test("should not mutate original date", () => {
|
||||
const original = new Date(2024, 0, 1); // Jan 1, 2024
|
||||
const db = new DateBuilder(original);
|
||||
db.addDays(10);
|
||||
const built = db.build();
|
||||
assert.equal(original.getFullYear(), 2024);
|
||||
assert.equal(original.getMonth(), 0); // January
|
||||
assert.equal(original.getDate(), 1); // Original date should remain unchanged
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 0); // January
|
||||
assert.equal(built.getDate(), 11); // New date should be Jan 11, 2024
|
||||
});
|
||||
|
||||
test("DateBuilder should handle leap years correctly", () => {
|
||||
const db = new DateBuilder(new Date(2020, 1, 29)); // Feb 29, 2020 (leap year)
|
||||
db.addYears(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2021);
|
||||
assert.equal(built.getMonth(), 2); // March
|
||||
assert.equal(built.getDate(), 1); // March 1, 2021 (not a leap year)
|
||||
});
|
||||
test("should handle leap years correctly", () => {
|
||||
const db = new DateBuilder(new Date(2020, 1, 29)); // Feb 29, 2020 (leap year)
|
||||
db.addYears(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2021);
|
||||
assert.equal(built.getMonth(), 2); // March
|
||||
assert.equal(built.getDate(), 1); // March 1, 2021 (not a leap year)
|
||||
});
|
||||
|
||||
test("DateBuilder should handle month overflow correctly", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 31)); // Jan 31, 2024
|
||||
db.addDays(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
assert.equal(built.getDate(), 1); // Feb 29, 2024 (leap year)
|
||||
test("should handle month overflow correctly", () => {
|
||||
const db = new DateBuilder(new Date(2024, 0, 31)); // Jan 31, 2024
|
||||
db.addDays(1);
|
||||
const built = db.build();
|
||||
assert.equal(built.getFullYear(), 2024);
|
||||
assert.equal(built.getMonth(), 1); // February
|
||||
assert.equal(built.getDate(), 1); // Feb 29, 2024 (leap year)
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,78 +1,80 @@
|
||||
import { test } from "node:test";
|
||||
import { describe, test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { ElapsedTime } from "./ElapsedTime";
|
||||
|
||||
test("ElapsedTime should be able to be initialised", () => {
|
||||
const db = new ElapsedTime(0n);
|
||||
assert.equal(db != null, true);
|
||||
});
|
||||
describe("ElapsedTime", () => {
|
||||
test("should be able to be initialised", () => {
|
||||
const db = new ElapsedTime(0n);
|
||||
assert.equal(db != null, true);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total nanoseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalNanoseconds, 1234567890n);
|
||||
});
|
||||
test("should return correct total nanoseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalNanoseconds, 1234567890n);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total microseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalMicroseconds, 1234567);
|
||||
});
|
||||
test("should return correct total microseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalMicroseconds, 1234567);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total milliseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalMilliseconds, 1234);
|
||||
});
|
||||
test("should return correct total milliseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.totalMilliseconds, 1234);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total seconds", () => {
|
||||
const db = new ElapsedTime(5000000000n);
|
||||
assert.equal(db.totalSeconds, 5);
|
||||
});
|
||||
test("should return correct total seconds", () => {
|
||||
const db = new ElapsedTime(5000000000n);
|
||||
assert.equal(db.totalSeconds, 5);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total minutes", () => {
|
||||
const db = new ElapsedTime(300000000000n); // 5 minutes
|
||||
assert.equal(db.totalMinutes, 5);
|
||||
});
|
||||
test("should return correct total minutes", () => {
|
||||
const db = new ElapsedTime(300000000000n); // 5 minutes
|
||||
assert.equal(db.totalMinutes, 5);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total hours", () => {
|
||||
const db = new ElapsedTime(7200000000000n); // 2 hours
|
||||
assert.equal(db.totalHours, 2);
|
||||
});
|
||||
test("should return correct total hours", () => {
|
||||
const db = new ElapsedTime(7200000000000n); // 2 hours
|
||||
assert.equal(db.totalHours, 2);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct total days", () => {
|
||||
const db = new ElapsedTime(172800000000000n); // 2 days
|
||||
assert.equal(db.totalDays, 2);
|
||||
});
|
||||
test("should return correct total days", () => {
|
||||
const db = new ElapsedTime(172800000000000n); // 2 days
|
||||
assert.equal(db.totalDays, 2);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct nanoseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.nanoseconds, 890);
|
||||
});
|
||||
test("should return correct nanoseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.nanoseconds, 890);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct microseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.microseconds, 567);
|
||||
});
|
||||
test("should return correct microseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.microseconds, 567);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct milliseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.milliseconds, 234);
|
||||
});
|
||||
test("should return correct milliseconds", () => {
|
||||
const db = new ElapsedTime(1234567890n);
|
||||
assert.equal(db.milliseconds, 234);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct seconds", () => {
|
||||
const db = new ElapsedTime(5000000000n);
|
||||
assert.equal(db.seconds, 5);
|
||||
});
|
||||
test("should return correct seconds", () => {
|
||||
const db = new ElapsedTime(5000000000n);
|
||||
assert.equal(db.seconds, 5);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct minutes", () => {
|
||||
const db = new ElapsedTime(300000000000n); // 5 minutes
|
||||
assert.equal(db.minutes, 5);
|
||||
});
|
||||
test("should return correct minutes", () => {
|
||||
const db = new ElapsedTime(300000000000n); // 5 minutes
|
||||
assert.equal(db.minutes, 5);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct hours", () => {
|
||||
const db = new ElapsedTime(7200000000000n); // 2 hours
|
||||
assert.equal(db.hours, 2);
|
||||
});
|
||||
test("should return correct hours", () => {
|
||||
const db = new ElapsedTime(7200000000000n); // 2 hours
|
||||
assert.equal(db.hours, 2);
|
||||
});
|
||||
|
||||
test("ElapsedTime should return correct days", () => {
|
||||
const db = new ElapsedTime(172800000000000n); // 2 days
|
||||
assert.equal(db.days, 2);
|
||||
test("should return correct days", () => {
|
||||
const db = new ElapsedTime(172800000000000n); // 2 days
|
||||
assert.equal(db.days, 2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
|
||||
// Discord.com sends ISO strings with +00:00 extension, not Z
|
||||
// This causes issues with Python bot libs
|
||||
const JSONReplacer = function (this: { [key: string]: unknown }, key: string, value: unknown) {
|
||||
import Stream from "node:stream";
|
||||
|
||||
export function JSONReplacer(this: { [key: string]: unknown }, key: string, value: unknown) {
|
||||
if (this[key] instanceof Date) {
|
||||
return (this[key] as Date).toISOString().replace("Z", "+00:00");
|
||||
}
|
||||
@@ -34,6 +36,4 @@ const JSONReplacer = function (this: { [key: string]: unknown }, key: string, va
|
||||
this[key] = this[key].toJSON();
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
export { JSONReplacer };
|
||||
}
|
||||
|
||||
@@ -1,52 +1,54 @@
|
||||
import { test } from "node:test";
|
||||
import { describe, test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { Stopwatch, timePromise } from "./Stopwatch";
|
||||
|
||||
test("Stopwatch should be able to be initialised", () => {
|
||||
const sw = new Stopwatch();
|
||||
assert.equal(sw != null, true);
|
||||
});
|
||||
|
||||
test("Stopwatch should measure elapsed time", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
const elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("Stopwatch should reset correctly", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
let elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
|
||||
sw.reset();
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
sw.stop();
|
||||
elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 50 && elapsed.totalMilliseconds < 100, `Elapsed time after reset was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("Stopwatch getElapsedAndReset should work correctly", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
let elapsed = sw.getElapsedAndReset();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
sw.stop();
|
||||
elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 50 && elapsed.totalMilliseconds < 100, `Elapsed time after getElapsedAndReset was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("timePromise should measure promise execution time", async () => {
|
||||
const { result, elapsed } = await timePromise(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
return 42;
|
||||
describe("Stopwatch", () => {
|
||||
test("should be able to be initialised", () => {
|
||||
const sw = new Stopwatch();
|
||||
assert.equal(sw != null, true);
|
||||
});
|
||||
|
||||
test("should measure elapsed time", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
const elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("should reset correctly", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
let elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
|
||||
sw.reset();
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
sw.stop();
|
||||
elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 49 && elapsed.totalMilliseconds < 100, `Elapsed time after reset was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("getElapsedAndReset should work correctly", async () => {
|
||||
const sw = Stopwatch.startNew();
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
sw.stop();
|
||||
let elapsed = sw.getElapsedAndReset();
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
sw.stop();
|
||||
elapsed = sw.elapsed();
|
||||
assert(elapsed.totalMilliseconds >= 50 && elapsed.totalMilliseconds < 100, `Elapsed time after getElapsedAndReset was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
test("timePromise should measure promise execution time", async () => {
|
||||
const { result, elapsed } = await timePromise(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 101));
|
||||
return 42;
|
||||
});
|
||||
assert.equal(result, 42);
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
assert.equal(result, 42);
|
||||
assert(elapsed.totalMilliseconds >= 100, `Elapsed time was ${elapsed.totalMilliseconds} ms`);
|
||||
});
|
||||
|
||||
+104
-102
@@ -1,120 +1,122 @@
|
||||
import { test } from "node:test";
|
||||
import { describe, test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { TimeSpan } from "./Timespan";
|
||||
|
||||
test("TimeSpan should be able to be initialised", () => {
|
||||
const db = new TimeSpan();
|
||||
assert.equal(db != null, true);
|
||||
});
|
||||
describe("TimeSpan", () => {
|
||||
test("should be able to be initialised", () => {
|
||||
const db = new TimeSpan();
|
||||
assert.equal(db != null, true);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to be initialised with start and end", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.start, now);
|
||||
assert.equal(ts.end, later);
|
||||
});
|
||||
test("should be able to be initialised with start and end", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.start, now);
|
||||
assert.equal(ts.end, later);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to be initialised with start and end (fromDates static method)", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = TimeSpan.fromDates(now, later);
|
||||
assert.equal(ts.start, now);
|
||||
assert.equal(ts.end, later);
|
||||
});
|
||||
test("should be able to be initialised with start and end (fromDates static method)", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = TimeSpan.fromDates(now, later);
|
||||
assert.equal(ts.start, now);
|
||||
assert.equal(ts.end, later);
|
||||
});
|
||||
|
||||
test("TimeSpan should throw error if start is greater than end", () => {
|
||||
assert.throws(() => {
|
||||
new TimeSpan(2000, 1000);
|
||||
}, /Start time must be less than or equal to end time./);
|
||||
});
|
||||
test("should throw error if start is greater than end", () => {
|
||||
assert.throws(() => {
|
||||
new TimeSpan(2000, 1000);
|
||||
}, /Start time must be less than or equal to end time./);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to return zero", () => {
|
||||
const ts = new TimeSpan();
|
||||
assert.equal(ts.totalMillis, 0);
|
||||
});
|
||||
test("should be able to return zero", () => {
|
||||
const ts = new TimeSpan();
|
||||
assert.equal(ts.totalMillis, 0);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to return timespan from milliseconds", () => {
|
||||
const ts = TimeSpan.fromMillis(1000);
|
||||
assert.equal(ts.totalMillis, 1000);
|
||||
assert.equal(ts.totalSeconds, 1);
|
||||
});
|
||||
test("should be able to return timespan from milliseconds", () => {
|
||||
const ts = TimeSpan.fromMillis(1000);
|
||||
assert.equal(ts.totalMillis, 1000);
|
||||
assert.equal(ts.totalSeconds, 1);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to return timespan from seconds", () => {
|
||||
const ts = TimeSpan.fromSeconds(60);
|
||||
assert.equal(ts.totalMillis, 60000);
|
||||
assert.equal(ts.totalSeconds, 60);
|
||||
assert.equal(ts.totalMinutes, 1);
|
||||
assert.equal(ts.minutes, 1);
|
||||
assert.equal(ts.hours, 0);
|
||||
assert.equal(ts.days, 0);
|
||||
});
|
||||
test("should be able to return timespan from seconds", () => {
|
||||
const ts = TimeSpan.fromSeconds(60);
|
||||
assert.equal(ts.totalMillis, 60000);
|
||||
assert.equal(ts.totalSeconds, 60);
|
||||
assert.equal(ts.totalMinutes, 1);
|
||||
assert.equal(ts.minutes, 1);
|
||||
assert.equal(ts.hours, 0);
|
||||
assert.equal(ts.days, 0);
|
||||
});
|
||||
|
||||
test("TimeSpan should be pure", () => {
|
||||
const count = 10;
|
||||
const timestamps = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
timestamps.push(TimeSpan.fromMillis(8972347984));
|
||||
for (const ts2 of timestamps) {
|
||||
assert.equal(ts2.totalMillis, 8972347984);
|
||||
assert.equal(ts2.totalSeconds, 8972347);
|
||||
assert.equal(ts2.totalMinutes, 149539);
|
||||
assert.equal(ts2.totalHours, 2492);
|
||||
assert.equal(ts2.totalDays, 103);
|
||||
assert.equal(ts2.totalWeeks, 14);
|
||||
assert.equal(ts2.totalMonths, 3);
|
||||
assert.equal(ts2.totalYears, 0);
|
||||
test("should be pure", () => {
|
||||
const count = 10;
|
||||
const timestamps = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
timestamps.push(TimeSpan.fromMillis(8972347984));
|
||||
for (const ts2 of timestamps) {
|
||||
assert.equal(ts2.totalMillis, 8972347984);
|
||||
assert.equal(ts2.totalSeconds, 8972347);
|
||||
assert.equal(ts2.totalMinutes, 149539);
|
||||
assert.equal(ts2.totalHours, 2492);
|
||||
assert.equal(ts2.totalDays, 103);
|
||||
assert.equal(ts2.totalWeeks, 14);
|
||||
assert.equal(ts2.totalMonths, 3);
|
||||
assert.equal(ts2.totalYears, 0);
|
||||
|
||||
assert.equal(ts2.millis, 984);
|
||||
assert.equal(ts2.seconds, 7);
|
||||
assert.equal(ts2.minutes, 19);
|
||||
assert.equal(ts2.hours, 20);
|
||||
assert.equal(ts2.days, 12);
|
||||
assert.equal(ts2.weekDays, 5);
|
||||
assert.equal(ts2.weeks, 1);
|
||||
assert.equal(ts2.months, 3);
|
||||
assert.equal(ts2.years, 0);
|
||||
assert.equal(ts2.millis, 984);
|
||||
assert.equal(ts2.seconds, 7);
|
||||
assert.equal(ts2.minutes, 19);
|
||||
assert.equal(ts2.hours, 20);
|
||||
assert.equal(ts2.days, 12);
|
||||
assert.equal(ts2.weekDays, 5);
|
||||
assert.equal(ts2.weeks, 1);
|
||||
assert.equal(ts2.months, 3);
|
||||
assert.equal(ts2.years, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to stringify", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toString(), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(true), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(true, false), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds");
|
||||
assert.equal(ts.toString(false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(false, false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds");
|
||||
});
|
||||
test("should be able to stringify", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toString(), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(true), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(true, false), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds");
|
||||
assert.equal(ts.toString(false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
||||
assert.equal(ts.toString(false, false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds");
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to shortStringify", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toShortString(), "3mo1w5d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(true), "3mo1w5d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(true, false), "3mo1w5d20h19m7s");
|
||||
assert.equal(ts.toShortString(false), "3mo12d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(false, false), "3mo12d20h19m7s");
|
||||
});
|
||||
test("should be able to shortStringify", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toShortString(), "3mo1w5d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(true), "3mo1w5d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(true, false), "3mo1w5d20h19m7s");
|
||||
assert.equal(ts.toShortString(false), "3mo12d20h19m7s984ms");
|
||||
assert.equal(ts.toShortString(false, false), "3mo12d20h19m7s");
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to shortStringify with spaces", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toShortString(undefined, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(true, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(true, false, true), "3mo 1w 5d 20h 19m 7s");
|
||||
assert.equal(ts.toShortString(false, undefined, true), "3mo 12d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(false, false, true), "3mo 12d 20h 19m 7s");
|
||||
});
|
||||
test("should be able to shortStringify with spaces", () => {
|
||||
const ts = TimeSpan.fromMillis(8972347984);
|
||||
assert.equal(ts.toShortString(undefined, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(true, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(true, false, true), "3mo 1w 5d 20h 19m 7s");
|
||||
assert.equal(ts.toShortString(false, undefined, true), "3mo 12d 20h 19m 7s 984ms");
|
||||
assert.equal(ts.toShortString(false, false, true), "3mo 12d 20h 19m 7s");
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to return start date", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.startDate.getTime(), now);
|
||||
});
|
||||
test("should be able to return start date", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.startDate.getTime(), now);
|
||||
});
|
||||
|
||||
test("TimeSpan should be able to return end date", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.endDate.getTime(), later);
|
||||
test("should be able to return end date", () => {
|
||||
const now = Date.now();
|
||||
const later = now + 5000;
|
||||
const ts = new TimeSpan(now, later);
|
||||
assert.equal(ts.endDate.getTime(), later);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user