/**
 * @vitest-environment jsdom
 */
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

const __dirname = dirname(fileURLToPath(import.meta.url));
const SCRIPT_PATH = join(__dirname, '../../static/assets/js/copyright-year.js');

describe('copyright-year.js', () => {
    let el: HTMLSpanElement;

    beforeEach(() => {
        el = document.createElement('span');
        el.id = 'copyright-year';
        el.textContent = 'placeholder';
        document.body.appendChild(el);
    });

    afterEach(() => {
        el.remove();
    });

    it('sets #copyright-year textContent to current year when element exists', () => {
        const code = readFileSync(SCRIPT_PATH, 'utf8');
         
        eval(code);

        const expected = new Date().getFullYear().toString();
        expect(el.textContent).toBe(expected);
    });

    it('does not throw when #copyright-year is missing', () => {
        el.remove();
        const code = readFileSync(SCRIPT_PATH, 'utf8');

        expect(() => {
             
            eval(code);
        }).not.toThrow();
    });
});
