import {test} from 'node:test';
import assert from 'node:assert/strict';
import {loadPage} from './learnrun.js';
const buttons = (page) => [...page.document.querySelectorAll('#thumbs li > button')];
const images = (page) => [...page.document.querySelectorAll('#thumbs li > button > img')];
test('Every photo gets a thumbnail button', async () => {
const page = await loadPage();
const indexes = buttons(page).map((b) => b.dataset.index);
assert.deepEqual(indexes, ['0', '1', '2'], 'the thumbnail buttons have the data-index values ' + JSON.stringify(indexes));
const types = buttons(page).map((b) => b.getAttribute('type'));
assert.deepEqual(types, ['button', 'button', 'button'], 'the thumbnail buttons have the types ' + JSON.stringify(types));
});
test('Each thumbnail shows its photo with alt text', async () => {
const page = await loadPage();
const alts = images(page).map((img) => img.getAttribute('alt'));
assert.deepEqual(
alts,
['A red fox in the snow', 'An owl on a branch', 'A heron by the river'],
'the thumbnail alt texts are ' + JSON.stringify(alts)
);
const data = images(page).map((img) => String(img.getAttribute('src')).startsWith('data:image/svg+xml,'));
assert.deepEqual(data, [true, true, true], 'a data: URL as src, per thumbnail: ' + JSON.stringify(data));
});
test('The main image shows the first photo', async () => {
const page = await loadPage();
const main = page.document.querySelector('#main');
const same = main.getAttribute('src') === images(page)[0].getAttribute('src');
assert.equal(same, true, 'the main image does not have the src of the first thumbnail');
const shown = main.getAttribute('alt');
assert.equal(shown, 'A red fox in the snow', 'the main image has the alt ' + JSON.stringify(shown));
});
test('The caption shows the alt text', async () => {
const page = await loadPage();
const text = page.document.querySelector('#caption').textContent;
assert.equal(text, 'A red fox in the snow', 'the caption shows ' + JSON.stringify(text));
});
test('Only the first thumbnail is marked as current', async () => {
const page = await loadPage();
const marks = buttons(page).map((b) => b.classList.contains('selected') + ' ' + b.getAttribute('aria-current'));
assert.deepEqual(marks, ['true true', 'false null', 'false null'], 'class selected and aria-current per thumbnail: ' + JSON.stringify(marks));
});