// Import Node's built-in test tools. const test = require("node:test"); const assert = require("node:assert/strict"); // Import the SSL service functions we want to test. const { checkSslCertificate, resolvePublicAddress, calculateDaysRemaining, formatCertificateName, } = require("../src/services/sslService"); // --------------------------------------------------------- // formatCertificateName() tests // --------------------------------------------------------- test("formats a complete certificate name", () => { const result = formatCertificateName({ CN: "example.com", O: "Example Organization", OU: "Technology", C: "US", ST: "Michigan", L: "Kalamazoo", }); assert.equal( result, "CN=example.com, O=Example Organization, OU=Technology, C=US, ST=Michigan, L=Kalamazoo" ); }); test("formats only certificate fields that exist", () => { const result = formatCertificateName({ CN: "example.com", O: "Example Organization", }); assert.equal( result, "CN=example.com, O=Example Organization" ); }); test("returns Unknown for missing certificate name data", () => { assert.equal(formatCertificateName(null), "Unknown"); assert.equal(formatCertificateName(undefined), "Unknown"); }); // --------------------------------------------------------- // calculateDaysRemaining() tests // --------------------------------------------------------- test("returns a positive number for a future expiration date", () => { const futureDate = new Date( Date.now() + 10 * 24 * 60 * 60 * 1000 ).toISOString(); const result = calculateDaysRemaining(futureDate); assert.equal(result >= 9 && result <= 10, true); }); test("returns a negative number for an expired certificate date", () => { const pastDate = new Date( Date.now() - 5 * 24 * 60 * 60 * 1000 ).toISOString(); const result = calculateDaysRemaining(pastDate); assert.equal(result < 0, true); }); test("returns null for an invalid certificate date", () => { assert.equal( calculateDaysRemaining("not-a-real-date"), null ); }); // --------------------------------------------------------- // resolvePublicAddress() tests // --------------------------------------------------------- test("resolves a public hostname to a public IP address", async () => { const result = await resolvePublicAddress("example.com"); assert.equal(typeof result.address, "string"); assert.equal([4, 6].includes(result.family), true); }); test("rejects a hostname that cannot be resolved", async () => { await assert.rejects( resolvePublicAddress( "this-domain-should-not-exist-bytegeist-12345.com" ), /hostname could not be resolved/i ); }); // --------------------------------------------------------- // checkSslCertificate() integration test // --------------------------------------------------------- test("checks a valid public SSL certificate", async () => { const result = await checkSslCertificate("example.com"); assert.equal(result.hostname, "example.com"); assert.equal(typeof result.resolvedAddress, "string"); assert.equal([4, 6].includes(result.addressFamily), true); assert.equal(typeof result.authorized, "boolean"); assert.equal(typeof result.subject, "string"); assert.equal(typeof result.issuer, "string"); assert.equal(typeof result.validFrom, "string"); assert.equal(typeof result.validTo, "string"); assert.equal(typeof result.checkedAt, "string"); assert.equal( result.daysRemaining === null || typeof result.daysRemaining === "number", true ); assert.equal( result.protocol === null || typeof result.protocol === "string", true ); assert.equal( result.cipher === null || typeof result.cipher === "string", true ); });