Repository: louislam/uptime-kuma
File: test/backend-test/test-domain.js (JavaScript)

Code before:
                    async () => await DomainExpiry.checkSupport(monitor),
                    (error) => {
                        assert.strictEqual(error.constructor.name, "TranslatableError");
                        assert.strictEqual(error.message, "domain_expiry_unsupported_unsupported_tld_no_rdap_endpoint");
                        return true;
                    }
                );
            });
        });
    });

    test("findByDomainNameOrCreate() retrieves expiration date for .com domain from RDAP", async () => {
        const domain = await DomainExpiry.findByDomainNameOrCreate("google.com");
        const expiryFromRdap = await domain.getExpiryDate(); // from RDAP
        assert.deepEqual(expiryFromRdap, new Date("2028-09-14T04:00:00.000Z"));
    });

    test("checkExpiry() caches expiration date in database", async () => {
        await DomainExpiry.checkExpiry("google.com"); // RDAP -> Cache
        const domain = await DomainExpiry.findByName("google.com");
        const diff = Date.now() - domain.lastCheck;
        assert(diff < 5 * 1000);
    });

    test("sendNotifications() triggers notification for expiring domain", async () => {
        await DomainExpiry.forMonitor(monHttpCom);
        const hook = {
            "port": 3010,
            "url": "capture"
        };
        const manyDays = 3650;
        await setSetting("domainExpiryNotifyDays", [ manyDays ], "general");
        const notif = R.convertToBean("notification", {
            "config": JSON.stringify({
                type: "webhook",
                httpMethod: "post",
                webhookContentType: "json",
                webhookURL: `http://127.0.0.1:${hook.port}/${hook.url}`
            }),
            "active": 1,
            "user_id": 1,
            "name": "Testhook"
        });
        const [ , data ] = await Promise.all([
            DomainExpiry.sendNotifications("google.com", [ notif ]),
            mockWebhook(hook.port, hook.url)
        ]);
        assert.match(data.msg, /will expire in/);
    });

    test("sendNotifications() handles domain with null expiry without sending NaN", async () => {

Review comment (suggestion):
```suggestion
        await DomainExpiry.findByName("google.com");
```

Code after:
                };
                await assert.rejects(
                    async () => await DomainExpiry.checkSupport(monitor),
                    (error) => {
                        assert.strictEqual(error.constructor.name, "TranslatableError");
                        assert.strictEqual(error.message, "domain_expiry_unsupported_unsupported_tld_no_rdap_endpoint");
                        return true;
                    }
                );
            });
        });
    });

    test("findByDomainNameOrCreate() retrieves expiration date for .com domain from RDAP", async () => {
        const domain = await DomainExpiry.findByDomainNameOrCreate("google.com");
        const expiryFromRdap = await domain.getExpiryDate(); // from RDAP
        assert.deepEqual(expiryFromRdap, new Date("2028-09-14T04:00:00.000Z"));
    });

    test("checkExpiry() caches expiration date in database", async () => {
        await DomainExpiry.checkExpiry("google.com"); // RDAP -> Cache
        const domain = await DomainExpiry.findByName("google.com");
        assert(dayjs.utc().diff(dayjs.utc(domain.lastCheck), "second") < 5);
    });

    test("sendNotifications() triggers notification for expiring domain", async () => {
        await DomainExpiry.findByName("google.com");
        const hook = {
            port: 3010,
            url: "capture",
        };
        const manyDays = 3650;
        await setSetting("domainExpiryNotifyDays", [manyDays], "general");
        const notif = R.convertToBean("notification", {
            config: JSON.stringify({
                type: "webhook",
                httpMethod: "post",
                webhookContentType: "json",
                webhookURL: `http://127.0.0.1:${hook.port}/${hook.url}`,
            }),
            active: 1,
            user_id: 1,
            name: "Testhook",
        });
        const [, data] = await Promise.all([
            DomainExpiry.sendNotifications("google.com", [notif]),
            mockWebhook(hook.port, hook.url),
        ]);
        assert.match(data.msg, /will expire in/);
    });