315 lines
20 KiB
JavaScript
315 lines
20 KiB
JavaScript
/**
|
||
* @file Valide le mode mobile de dépannage de Sokko TCG dans un téléphone simulé (écran 393 × 852 d'un iPhone 14 Pro,
|
||
* tactile) : redirection, cadre sans le shell du site, sections sans élément coupé, boutons sur une ligne,
|
||
* ouverture d'un paquet et expédition au toucher.
|
||
*
|
||
* Contexte : projet `phone-cards` de playwright.cards.config.js, lancé après les parcours ordinateur
|
||
* (nebula-cards.spec.js) sur le même serveur et la même base : il consomme des ouvertures du joueur de
|
||
* développement, ce que les parcours ordinateur ne doivent pas voir. Ceux-ci laissent ce joueur en expédition :
|
||
* le parcours des Expéditions avance l'horloge du serveur d'un jour pour la récupérer, et la remet à zéro à la fin.
|
||
* Celui de l'Équipe du jour l'avance de dix jours (au-delà des cinq du parcours ordinateur), pour une Carte du jour
|
||
* neuve, puis referme l'Équipe et remet l'horloge à zéro.
|
||
* @see DESIGN_SYSTEM.md (section 24, « Mode mobile de dépannage »)
|
||
*/
|
||
|
||
import { readFile, writeFile } from "node:fs/promises";
|
||
import { DatabaseSync } from "node:sqlite";
|
||
import { tmpdir } from "node:os";
|
||
import { join } from "node:path";
|
||
import { expect, test } from "@playwright/test";
|
||
|
||
const WIDTH = 393;
|
||
const MOBILE_SECTIONS = ["home", "guild", "daily", "team", "race", "mirror", "expeditions", "packs", "collection", "shop", "transfers"];
|
||
const setServerClock = (offsetMs) => writeFile(join(tmpdir(), "sokkog-cards-e2e-clock.json"), JSON.stringify({ offsetMs }));
|
||
|
||
async function loginOnPhone(page, route = "/#/cards") {
|
||
await page.goto(route);
|
||
await page.getByRole("button", { name: "Version développement", exact: true }).click();
|
||
await expect(page.locator(".nebula-mobile-dashboard")).toBeVisible();
|
||
}
|
||
|
||
// Un bouton dont un texte revient à la ligne est trop serré. Chaque texte est mesuré seul : une icône ou un prix
|
||
// plus gros à côté ne compte pas comme une deuxième ligne. Les tuiles qui empilent volontairement un titre et un
|
||
// sous-titre (choix d'indice, paquets) ne sont pas des boutons d'action `.nds-button`.
|
||
async function expectButtonsOnOneLine(page) {
|
||
const wrapped = await page.locator("main .nds-button").evaluateAll((buttons) => buttons.filter((button) => {
|
||
const document = button.ownerDocument;
|
||
const texts = document.createTreeWalker(button, 4);
|
||
while (texts.nextNode()) {
|
||
const range = document.createRange();
|
||
range.selectNodeContents(texts.currentNode);
|
||
if (new Set([...range.getClientRects()].filter((rect) => rect.width > 1).map((rect) => Math.round(rect.top))).size > 1) return true;
|
||
}
|
||
return false;
|
||
}).map((button) => button.textContent.trim()));
|
||
expect(wrapped).toEqual([]);
|
||
}
|
||
|
||
// Un ancêtre peut masquer ce qui dépasse : seule la position des éléments révèle un contenu coupé.
|
||
async function expectNothingClipped(page) {
|
||
const clipped = await page.locator("main :is(a, button, input, h1, h2, h3, h4, p, li, img, .nebula-card, .nds-badge)").evaluateAll((elements) => elements
|
||
.filter((element) => {
|
||
const box = element.getBoundingClientRect();
|
||
return box.width > 0 && (box.right > element.ownerDocument.defaultView.innerWidth + 0.5 || box.left < -0.5);
|
||
})
|
||
.map((element) => element.textContent.trim().slice(0, 40)));
|
||
expect(clipped).toEqual([]);
|
||
}
|
||
|
||
test("a phone opening the game lands in the mobile mode, without the site shell, and every section fits the screen", async ({ page }) => {
|
||
await loginOnPhone(page, "/#/cards/packs");
|
||
// La redirection garde la section demandée et remplace l'adresse.
|
||
await expect(page).toHaveURL(/#\/mobile\/cards\/packs$/);
|
||
await expect(page.locator(".sidebar, .topbar")).toHaveCount(0);
|
||
const tabs = page.getByRole("navigation", { name: "Sections de Sokko TCG" });
|
||
await expect(tabs.getByRole("link")).toHaveText([/^Accueil/, /^Guilde/, /^Paquets/, /^Cartes/, /^Boutique/, /^Échanges/]);
|
||
await expect(tabs.getByRole("link", { name: /^Paquets/ })).toHaveAttribute("aria-current", "page");
|
||
|
||
for (const section of MOBILE_SECTIONS) {
|
||
await page.goto(`/#/mobile/cards/${section}`);
|
||
await expect(page.locator(".nebula-dashboard-section")).toBeVisible();
|
||
expect(await page.evaluate(() => globalThis.document.documentElement.scrollWidth), section).toBeLessThanOrEqual(WIDTH);
|
||
await expectNothingClipped(page);
|
||
await expectButtonsOnOneLine(page);
|
||
// Aucun lien de la page ne ramène vers la version complète, sauf celui qui le propose.
|
||
const fullLinks = await page.locator("main a[href^='#/cards']").evaluateAll((links) => links.map((link) => link.textContent.trim()));
|
||
expect(fullLinks, section).toEqual(["Version complète"]);
|
||
}
|
||
// L'affichage Stats de la collection tient aussi dans l'écran : son tableau devient une fiche par carte.
|
||
await page.goto("/#/mobile/cards/collection");
|
||
await page.getByRole("tab", { name: "Stats", exact: true }).click();
|
||
await page.getByRole("button", { name: "Orientation", exact: true }).click();
|
||
await expect(page.getByRole("region", { name: "Stats des cartes" })).toBeVisible();
|
||
expect(await page.evaluate(() => globalThis.document.documentElement.scrollWidth)).toBeLessThanOrEqual(WIDTH);
|
||
await expectNothingClipped(page);
|
||
await expectButtonsOnOneLine(page);
|
||
// Les quêtes de la Guilde allument l'onglet Guilde.
|
||
await page.goto("/#/mobile/cards/daily");
|
||
await expect(tabs.getByRole("link", { name: /^Guilde/ })).toHaveAttribute("aria-current", "page");
|
||
});
|
||
|
||
test("sections kept on the computer point to the full version, which a phone can leave for the mobile mode again", async ({ page }) => {
|
||
await loginOnPhone(page, "/#/mobile/cards/account");
|
||
await expect(page.getByRole("heading", { name: "Disponible sur ordinateur", exact: true })).toBeVisible();
|
||
await expect(page.getByRole("link", { name: "Ouvrir la version complète", exact: true })).toHaveAttribute("href", "#/cards/account");
|
||
|
||
// « Version complète » est retenu : la redirection ne ramène plus le téléphone en mode mobile.
|
||
await page.goto("/#/mobile/cards/guild");
|
||
await page.getByRole("link", { name: "Version complète", exact: true }).tap();
|
||
await expect(page).toHaveURL(/#\/cards\/guild$/);
|
||
await expect(page.locator(".sidebar")).toBeVisible();
|
||
await page.goto("/#/cards/packs");
|
||
await expect(page.getByRole("heading", { name: "Paquets permanents", exact: true })).toBeVisible();
|
||
await expect(page).toHaveURL(/#\/cards\/packs$/);
|
||
|
||
// Sur téléphone, la version complète propose de revenir au mode mobile, ce qui efface le choix.
|
||
await page.getByRole("link", { name: "Version mobile", exact: true }).tap();
|
||
await expect(page).toHaveURL(/#\/mobile\/cards\/packs$/);
|
||
await page.goto("/#/cards/shop");
|
||
await expect(page).toHaveURL(/#\/mobile\/cards\/shop$/);
|
||
});
|
||
|
||
test("a computer window, even narrow, is never sent to the mobile mode", async ({ browser }) => {
|
||
const context = await browser.newContext({ viewport: { width: 768, height: 900 }, hasTouch: false, isMobile: false });
|
||
const page = await context.newPage();
|
||
await page.goto("/#/cards/packs");
|
||
await expect(page.getByRole("button", { name: "Version développement", exact: true })).toBeVisible();
|
||
await expect(page).toHaveURL(/#\/cards\/packs$/);
|
||
await expect(page.locator(".sidebar")).toBeVisible();
|
||
await context.close();
|
||
});
|
||
|
||
test("a phone picks a pack among small backs, then reveals its three cards side by side, named once revealed", async ({ page }) => {
|
||
await loginOnPhone(page, "/#/mobile/cards/packs");
|
||
// Le choix du paquet montre de petits dos, trois par rangée.
|
||
const backWidth = await page.locator(".nebula-pack-back").first().evaluate((element) => element.getBoundingClientRect().width);
|
||
expect(backWidth).toBeGreaterThan(80);
|
||
expect(backWidth).toBeLessThan(WIDTH / 3);
|
||
const open = page.getByRole("button", { name: /^Ouvrir ce paquet/ }).first();
|
||
await open.scrollIntoViewIfNeeded();
|
||
await open.tap();
|
||
await expect(page.getByRole("heading", { name: "Révélez vos trois cartes" })).toBeVisible();
|
||
await expectButtonsOnOneLine(page);
|
||
const cards = page.locator(".nebula-opening-cards .nebula-card");
|
||
await expect(cards).toHaveCount(3);
|
||
// Tout le tirage d'un coup d'œil : trois petites cartes sur une rangée, dans l'écran.
|
||
const boxes = await cards.evaluateAll((elements) => elements.map((element) => element.getBoundingClientRect()).map(({ left, right, top }) => ({ left, right, top })));
|
||
expect(new Set(boxes.map((box) => Math.round(box.top))).size).toBe(1);
|
||
expect(boxes.every((box, index) => box.right <= WIDTH && (index === 0 || box.left >= boxes[index - 1].right))).toBe(true);
|
||
const names = page.locator(".nebula-opening-names li");
|
||
await expect(names.first()).toBeEmpty();
|
||
// Les actions changent sans déplacer les cartes : « Tout révéler » garde sa place jusqu'aux boutons de fin.
|
||
const cardsTop = () => page.locator(".nebula-opening-cards").evaluate((element) => element.getBoundingClientRect().top + element.ownerDocument.defaultView.scrollY);
|
||
const topBefore = await cardsTop();
|
||
await cards.first().tap();
|
||
await page.getByRole("button", { name: "Tout révéler" }).tap();
|
||
await expect(page.getByRole("button", { name: "Retour", exact: true })).toBeEnabled({ timeout: 30_000 });
|
||
expect(await cardsTop()).toBe(topBefore);
|
||
// Le petit format n'affiche pas le nom : il apparaît sous chaque carte révélée, sous le badge de sa rareté.
|
||
for (const name of await names.all()) {
|
||
await expect(name.locator("> :first-child")).toHaveClass(/nebula-opening-rarity/);
|
||
await expect(name.locator(".nebula-opening-rarity")).not.toBeEmpty();
|
||
await expect(name.locator("strong")).not.toBeEmpty();
|
||
}
|
||
await expect(page.getByRole("button", { name: "Ouvrir un autre paquet", exact: true })).toBeVisible();
|
||
// Une carte révélée se touche pour la voir en grand ; « Fermer » ou le fond ramènent à l'ouverture.
|
||
const revealedName = await names.first().locator("strong").textContent();
|
||
await page.getByRole("button", { name: `Voir ${revealedName} en grand`, exact: true }).tap();
|
||
const zoom = page.getByRole("dialog", { name: revealedName, exact: true });
|
||
await expect(zoom.locator(".nebula-card[data-card-size='lg']")).toBeVisible();
|
||
expect(await zoom.locator(".nebula-card").evaluate((element) => element.getBoundingClientRect().width)).toBeGreaterThan(280);
|
||
await zoom.getByRole("button", { name: "Fermer", exact: true }).tap();
|
||
await expect(zoom).toHaveCount(0);
|
||
await expectButtonsOnOneLine(page);
|
||
expect(await page.evaluate(() => globalThis.document.documentElement.scrollWidth)).toBeLessThanOrEqual(WIDTH);
|
||
});
|
||
|
||
test("a phone sends a card on an expedition, then claims its loot, all by touch", async ({ page }) => {
|
||
test.setTimeout(120_000);
|
||
const day = 24 * 3_600_000;
|
||
try {
|
||
// Un jour plus tard, toute expédition laissée en route par les parcours ordinateur est rentrée.
|
||
await setServerClock(day);
|
||
await loginOnPhone(page, "/#/mobile/cards/guild");
|
||
// Le joueur de développement est administrateur : il ouvre les Expéditions si aucun parcours ne l'a fait
|
||
// (l'administration n'est jamais redirigée vers le mode mobile).
|
||
await page.goto("/#/cards/admin/guild/expeditions");
|
||
const status = page.getByRole("region", { name: "Expéditions", exact: true });
|
||
await expect(status).toContainText(/Ouvertes aux joueurs|Fermées aux joueurs/);
|
||
const openQuest = status.getByRole("button", { name: "Ouvrir les Expéditions", exact: true });
|
||
if (await openQuest.count()) {
|
||
await openQuest.click();
|
||
await page.getByRole("dialog").getByRole("button", { name: "Ouvrir", exact: true }).click();
|
||
await expect(status).toContainText("Ouvertes aux joueurs");
|
||
}
|
||
await page.goto("/#/mobile/cards/guild");
|
||
await page.getByRole("article", { name: "Expéditions" }).getByRole("link").first().tap();
|
||
await expect(page).toHaveURL(/#\/mobile\/cards\/expeditions$/);
|
||
|
||
// Selon les parcours passés avant : la carte offerte à la première visite, un butin à récupérer, ou la préparation.
|
||
const destinations = page.getByRole("region", { name: "1. Destination du jour" });
|
||
const gift = page.getByRole("dialog", { name: "Votre première carte de destination" });
|
||
const claim = page.getByRole("button", { name: "Récupérer le butin", exact: true });
|
||
await expect(destinations.or(gift).or(claim.first())).toBeVisible();
|
||
if (await gift.isVisible()) {
|
||
await gift.locator(".nebula-card").first().tap();
|
||
await gift.getByRole("button", { name: "Préparer mon expédition", exact: true }).tap();
|
||
}
|
||
// Chaque butin récupéré retire son bouton : attendre qu'il soit parti avant de compter le suivant.
|
||
for (let left = await claim.count(); left > 0; left = await claim.count()) {
|
||
await claim.first().tap();
|
||
await expect(claim).toHaveCount(left - 1);
|
||
}
|
||
await expect(destinations).toBeVisible();
|
||
// Le Sac est replié par défaut sur téléphone.
|
||
if (await page.locator(".nebula-expedition-bag").count()) await expect(page.getByRole("button", { name: "Ouvrir le Sac", exact: true })).toBeVisible();
|
||
|
||
await destinations.locator(".nebula-expedition-destination").first().tap();
|
||
await page.getByRole("region", { name: "2. Votre explorateur" }).getByRole("button", { name: "Choisir un explorateur", exact: true }).tap();
|
||
// Choix de l'explorateur : une carte par ligne, ligne entière touchable ; la recherche, les deux tris et les quatre stats du
|
||
// build tiennent à l'écran, et la liste commence dans la première moitié de l'écran.
|
||
const picker = page.getByRole("dialog");
|
||
const firstChoice = picker.getByRole("button", { name: /^Choisir / }).first();
|
||
expect((await firstChoice.boundingBox()).width).toBeGreaterThan(WIDTH * 0.7);
|
||
await expect(picker.getByRole("group", { name: "Trier par" }).getByRole("button")).toHaveCount(2);
|
||
await expect(picker.getByRole("group", { name: "Build" }).getByRole("button")).toHaveCount(4);
|
||
for (const sort of await picker.locator(".nebula-picker-order").getByRole("button").all()) await expect(sort).toBeInViewport();
|
||
await expect(picker.getByRole("combobox", { name: "Rechercher par nom ou par étiquette" })).toBeInViewport();
|
||
expect((await firstChoice.boundingBox()).y).toBeLessThan(page.viewportSize().height / 2);
|
||
await firstChoice.tap();
|
||
const settings = page.getByRole("region", { name: "3. Durée et départ" });
|
||
await settings.getByRole("button", { name: "2 h", exact: true }).tap();
|
||
const depart = settings.getByRole("button", { name: "Partir seul maintenant", exact: true });
|
||
await depart.scrollIntoViewIfNeeded();
|
||
await depart.tap();
|
||
await expect(page.locator(".nebula-expedition-journey")).toContainText(/En route/);
|
||
await expectNothingClipped(page);
|
||
|
||
await setServerClock(day + 3 * 3_600_000);
|
||
await page.reload();
|
||
await claim.first().scrollIntoViewIfNeeded();
|
||
await claim.first().tap();
|
||
await expect(destinations).toBeVisible();
|
||
} finally {
|
||
await setServerClock(0);
|
||
}
|
||
});
|
||
|
||
test("a phone picks a team companion by touch, reads its stats, then reveals a star among large ones, with full-width actions", async ({ page }) => {
|
||
test.setTimeout(120_000);
|
||
const { path: databasePath } = JSON.parse(await readFile(join(tmpdir(), "sokkog-cards-e2e-db.json"), "utf8"));
|
||
const status = page.getByRole("region", { name: "Équipe du jour", exact: true });
|
||
try {
|
||
await setServerClock(10 * 24 * 3_600_000);
|
||
await loginOnPhone(page, "/#/mobile/cards/guild");
|
||
// L'administration n'est jamais redirigée : le joueur de développement y ouvre l'Équipe du jour.
|
||
await page.goto("/#/cards/admin/guild/team");
|
||
await expect(status).toContainText(/Ouverte aux joueurs|Fermée aux joueurs/);
|
||
const openQuest = status.getByRole("button", { name: "Ouvrir l’Équipe du jour", exact: true });
|
||
if (await openQuest.count()) {
|
||
await openQuest.click();
|
||
await page.getByRole("dialog").getByRole("button", { name: "Ouvrir", exact: true }).click();
|
||
await expect(status).toContainText("Ouverte aux joueurs");
|
||
}
|
||
|
||
// Recruter demande d'avoir joué la Carte du jour : sa réponse est lue dans la base, comme sur ordinateur.
|
||
await page.goto("/#/mobile/cards/daily");
|
||
const input = page.getByRole("combobox", { name: "Quelle est cette carte ?" });
|
||
await expect(input).toBeVisible();
|
||
const database = new DatabaseSync(databasePath, { readOnly: true });
|
||
const { card_id: answerId } = database.prepare("SELECT attempt.card_id FROM daily_attempts AS attempt JOIN players ON players.id = attempt.player_id WHERE players.discord_id = 'dev-admin' ORDER BY attempt.day DESC LIMIT 1").get();
|
||
database.close();
|
||
const answer = await page.evaluate(async (cardId) => (await (await globalThis.fetch("/api/cards/config")).json()).catalog.cards.find((card) => card.id === cardId).name, answerId);
|
||
await input.fill(answer);
|
||
await page.getByRole("button", { name: "Proposer", exact: true }).tap();
|
||
const reward = page.getByRole("region", { name: "Choisissez votre récompense", exact: true });
|
||
await expect(page.locator(".nebula-daily-result").getByRole("heading", { name: /^Trouvée/ })).toBeVisible();
|
||
if (await reward.count()) await reward.getByRole("button").first().tap();
|
||
|
||
await page.goto("/#/mobile/cards/team");
|
||
await page.getByRole("button", { name: "Recruter une équipe", exact: true }).tap();
|
||
const offer = page.getByRole("region", { name: "Choisissez un compagnon à recruter", exact: true });
|
||
// Trois cartes à choisir dans l'écran ; « Recruter » attend un choix, qui affiche la fiche de la carte.
|
||
const cards = offer.locator("button.nebula-team-companion");
|
||
await expect(cards).toHaveCount(3);
|
||
expect(await page.evaluate(() => globalThis.document.documentElement.scrollWidth)).toBeLessThanOrEqual(WIDTH);
|
||
await expectNothingClipped(page);
|
||
const recruit = offer.locator(".nebula-team-offer-detail").getByRole("button");
|
||
await expect(recruit).toBeDisabled();
|
||
await cards.nth(1).tap();
|
||
await expect(cards.nth(1)).toHaveAttribute("aria-pressed", "true");
|
||
// Une carte dont la spécialité manque à l'équipe porte une pastille « ! » et l'annonce après son nom.
|
||
const name = (await cards.nth(1).getAttribute("aria-label")).replace(/, Manque à votre équipe$/, "");
|
||
await expect(offer.getByRole("heading", { name, exact: true })).toBeVisible();
|
||
await expect(offer.locator(".nebula-team-offer-stats li")).toHaveCount(4);
|
||
await expectButtonsOnOneLine(page);
|
||
await expect(recruit).toHaveAccessibleName(`Recruter ${name}`);
|
||
await recruit.tap();
|
||
|
||
// Amélioration : des étoiles d'au moins 64 px, qui ne se chevauchent pas, et le compagnon en bandeau.
|
||
await expect(page.getByRole("heading", { name: "Voulez-vous améliorer ce compagnon ?", exact: true })).toBeVisible();
|
||
const sky = page.getByRole("group", { name: "Étoiles", exact: true });
|
||
const stars = await sky.getByRole("button").evaluateAll((buttons) => buttons.map((button) => button.getBoundingClientRect()).map(({ left, right, top, bottom, width }) => ({ left, right, top, bottom, width })));
|
||
expect(Math.min(...stars.map((star) => star.width))).toBeGreaterThanOrEqual(64);
|
||
const overlapping = stars.some((star, index) => stars.slice(index + 1).some((other) => star.left < other.right && other.left < star.right && star.top < other.bottom && other.top < star.bottom));
|
||
expect(overlapping).toBe(false);
|
||
expect(await page.locator(".nebula-team-improve > .nebula-team-companion").evaluate((element) => element.getBoundingClientRect().height)).toBeLessThan(180);
|
||
await expectNothingClipped(page);
|
||
await sky.getByRole("button", { name: "Étoile cachée 1", exact: true }).tap();
|
||
await expect(sky.getByRole("button", { name: "Étoile cachée 1", exact: true })).toHaveCount(0);
|
||
// Les actions sont empilées sur toute la largeur, chacune sur une ligne.
|
||
const actions = await page.locator(".nebula-team-actions .nds-button").evaluateAll((buttons) => buttons.map((button) => button.getBoundingClientRect().width));
|
||
const panelWidth = await page.locator(".nebula-team-actions").evaluate((element) => element.getBoundingClientRect().width);
|
||
expect(actions.every((width) => width >= panelWidth - 1)).toBe(true);
|
||
await expectButtonsOnOneLine(page);
|
||
} finally {
|
||
await page.goto("/#/cards/admin/guild/team");
|
||
const closeQuest = status.getByRole("button", { name: "Fermer l’Équipe du jour", exact: true });
|
||
if (await closeQuest.count()) {
|
||
await closeQuest.click();
|
||
await page.getByRole("dialog").getByRole("button", { name: "Fermer", exact: true }).click();
|
||
await expect(status).toContainText("Fermée aux joueurs");
|
||
}
|
||
await setServerClock(0);
|
||
}
|
||
});
|