2026-09-08 14:33:51 +02:00
|
|
|
/**
|
|
|
|
|
* Badge Presenza fissa end-to-end contro il database locale:
|
|
|
|
|
* `bun test/integration/presenze-badge.test.ts`.
|
|
|
|
|
*
|
|
|
|
|
* I test unitari (`test/unit/presenze.test.ts`, `test/unit/badges.test.ts`) verificano
|
|
|
|
|
* `contaPresenzeGiocatore()` e `statoBadge()` come funzioni pure, con eventi e risposte
|
|
|
|
|
* costruiti a mano. Qui invece si scrivono eventi e risposte veri su `eventi_app`/
|
|
|
|
|
* `risposte_presenze`, si rileggono via REST con la stessa forma di `fetchPresenze()`/
|
|
|
|
|
* `daRiga()`, e si passa il risultato attraverso `contaPresenzeGiocatore()` fino a
|
|
|
|
|
* `statoBadge()` sul badge `presenze`: se una colonna cambia nome o la mappatura si rompe, qui
|
|
|
|
|
* il grado del badge torna sbagliato anche se i test unitari restano verdi, perché quelli non
|
|
|
|
|
* toccano mai il database.
|
|
|
|
|
*
|
|
|
|
|
* Copre anche un punto verificato nell'analisi ma non ovvio: **non serve nessun controllo a
|
|
|
|
|
* database sui convocati** per questo badge (a differenza di MVP/pagelle/badge social, vedi
|
|
|
|
|
* M13 in `docs/modules/badge.md`) perché `contaPresenzeGiocatore()` filtra già gli eventi per
|
|
|
|
|
* `convocati` lato applicazione — una risposta scritta per un evento a cui non si era
|
|
|
|
|
* convocati non conta comunque, anche se la riga esiste nel database.
|
|
|
|
|
*
|
|
|
|
|
* Gira solo sullo stack locale (`npx supabase start`) e cancella le proprie righe alla fine:
|
|
|
|
|
* usa id con il prefisso `test-presenze-badge`, che nessun dato vero può avere.
|
|
|
|
|
*/
|
|
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import { badgeDefs, statoBadge } from "@/lib/badges";
|
|
|
|
|
import { contaPresenzeGiocatore } from "@/lib/presenze";
|
|
|
|
|
import { daRiga, type RigaEvento, type Evento } from "@/lib/eventi";
|
|
|
|
|
import { giocatori, type Giocatore } from "@/lib/crapp-data";
|
|
|
|
|
import { statoLocale } from "../helpers/locale";
|
|
|
|
|
import { prova, riepilogo, salta } from "../helpers/prova";
|
2026-09-24 12:14:37 +02:00
|
|
|
import type { Stato } from "@/lib/crapp-data";
|
|
|
|
|
import type { MappaPresenze } from "@/lib/presenze";
|
2026-09-08 14:33:51 +02:00
|
|
|
|
|
|
|
|
const locale = statoLocale();
|
|
|
|
|
|
|
|
|
|
if (!locale) {
|
|
|
|
|
salta("badge presenze sul database", "stack locale non attivo (npx supabase start)");
|
|
|
|
|
riepilogo("presenze-badge");
|
|
|
|
|
} else {
|
|
|
|
|
const { url: URL_BASE, servizio: SERVIZIO } = locale;
|
|
|
|
|
console.log(`badge presenze su ${URL_BASE}`);
|
|
|
|
|
|
|
|
|
|
const PREFISSO = "test-presenze-badge";
|
|
|
|
|
const presenzeDef = badgeDefs.find((b) => b.id === "presenze")!;
|
|
|
|
|
// "Oggi" fissato nel futuro: gli eventi scritti sotto sono datati nel passato remoto, così
|
|
|
|
|
// restano "passati" (quindi contati) a prescindere da quando gira il test.
|
|
|
|
|
const OGGI = "2099-01-01";
|
|
|
|
|
|
|
|
|
|
const rest = (percorso: string, init?: RequestInit) =>
|
|
|
|
|
fetch(`${URL_BASE}/rest/v1/${percorso}`, {
|
|
|
|
|
...init,
|
|
|
|
|
headers: {
|
|
|
|
|
apikey: SERVIZIO,
|
|
|
|
|
Authorization: `Bearer ${SERVIZIO}`,
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
...(init?.headers ?? {}),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** `2020-01-01` per i=1, avanzando di un giorno per ogni evento: niente collisioni di data. */
|
|
|
|
|
const dataEvento = (i: number) => {
|
|
|
|
|
const d = new Date(Date.UTC(2020, 0, 1));
|
|
|
|
|
d.setUTCDate(d.getUTCDate() + i - 1);
|
|
|
|
|
return d.toISOString().slice(0, 10);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function creaEvento(id: string, i: number, convocati?: string[]) {
|
|
|
|
|
const res = await rest("eventi_app", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { Prefer: "resolution=merge-duplicates,return=representation" },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
id,
|
|
|
|
|
tipo: "allenamento",
|
|
|
|
|
titolo: `Allenamento ${id}`,
|
|
|
|
|
data: dataEvento(i),
|
|
|
|
|
...(convocati ? { convocati } : {}),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) throw new Error(`creazione evento ${id}: ${res.status} ${await res.text()}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function rispondi(eventoId: string, giocatoreId: string, stato: string) {
|
|
|
|
|
const res = await rest("risposte_presenze?on_conflict=evento_id,giocatore_id", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { Prefer: "resolution=merge-duplicates,return=representation" },
|
|
|
|
|
body: JSON.stringify({ evento_id: eventoId, giocatore_id: giocatoreId, stato }),
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) throw new Error(`upsert su risposte_presenze: ${res.status} ${await res.text()}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Rilegge esattamente come fa l'app: `daRiga()` per gli eventi. */
|
|
|
|
|
async function leggiEventi(): Promise<Evento[]> {
|
|
|
|
|
const res = await rest(
|
|
|
|
|
"eventi_app?id=like." +
|
|
|
|
|
PREFISSO +
|
|
|
|
|
"*&select=id,tipo,titolo,luogo,data,ora,note,convocati,campionato,casa,pagelle_chiuse,creato_il",
|
|
|
|
|
);
|
|
|
|
|
const righe = (await res.json()) as RigaEvento[];
|
|
|
|
|
return righe.map(daRiga);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Rilegge esattamente come `fetchPresenze()`. */
|
2026-09-24 12:14:37 +02:00
|
|
|
async function leggiPresenze(): Promise<MappaPresenze> {
|
2026-09-08 14:33:51 +02:00
|
|
|
const res = await rest(
|
|
|
|
|
`risposte_presenze?evento_id=like.${PREFISSO}*&select=evento_id,giocatore_id,stato`,
|
|
|
|
|
);
|
|
|
|
|
const righe = (await res.json()) as Array<{
|
|
|
|
|
evento_id: string;
|
|
|
|
|
giocatore_id: string;
|
2026-09-24 12:14:37 +02:00
|
|
|
stato: Stato;
|
2026-09-08 14:33:51 +02:00
|
|
|
}>;
|
2026-09-24 12:14:37 +02:00
|
|
|
const mappa: MappaPresenze = {};
|
2026-09-08 14:33:51 +02:00
|
|
|
for (const r of righe) (mappa[r.evento_id] ??= {})[r.giocatore_id] = r.stato;
|
|
|
|
|
return mappa;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Un giocatore azzerato, come in `test/unit/badges.test.ts`. */
|
|
|
|
|
function giocatoreAzzerato(presenze: number): Giocatore {
|
|
|
|
|
return { ...giocatori[0]!, presenze, mvp: 0, mediaVoto: 0, votiPagella: 0, palloni: 0 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await prova(
|
|
|
|
|
"il badge presenze passa da bronzo a oro con presenze vere lette dal database",
|
|
|
|
|
async () => {
|
2026-11-15 20:02:30 +01:00
|
|
|
// Tanti allenamenti passati quanti ne chiede l'oro. "pp1" risponde a tutti (un "ritardo"
|
|
|
|
|
// nel mezzo per verificare che conti come presenza), così una sola scrittura basta a
|
|
|
|
|
// verificare le tre soglie.
|
|
|
|
|
const { bronzo, argento, oro } = presenzeDef.soglie;
|
|
|
|
|
for (let i = 1; i <= oro; i += 1) {
|
2026-09-08 14:33:51 +02:00
|
|
|
await creaEvento(`${PREFISSO}-a${String(i).padStart(2, "0")}`, i);
|
|
|
|
|
}
|
2026-11-15 20:02:30 +01:00
|
|
|
for (let i = 1; i <= oro; i += 1) {
|
2026-09-08 14:33:51 +02:00
|
|
|
const stato = i === 10 ? "ritardo" : "presente";
|
|
|
|
|
await rispondi(`${PREFISSO}-a${String(i).padStart(2, "0")}`, "pp1", stato);
|
|
|
|
|
}
|
2026-11-15 20:02:30 +01:00
|
|
|
// "pp2" risponde solo ai primi `bronzo`: resta a bronzo, non oltre.
|
|
|
|
|
for (let i = 1; i <= bronzo; i += 1) {
|
2026-09-08 14:33:51 +02:00
|
|
|
await rispondi(`${PREFISSO}-a${String(i).padStart(2, "0")}`, "pp2", "presente");
|
|
|
|
|
}
|
|
|
|
|
// "pp2" risponde anche "assente" a un evento: non deve contare.
|
2026-11-15 20:02:30 +01:00
|
|
|
await rispondi(`${PREFISSO}-a${String(bronzo + 1).padStart(2, "0")}`, "pp2", "assente");
|
2026-09-08 14:33:51 +02:00
|
|
|
|
|
|
|
|
const eventi = await leggiEventi();
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(eventi.length, oro, "tutti gli eventi scritti si rileggono");
|
2026-09-08 14:33:51 +02:00
|
|
|
const presenze = await leggiPresenze();
|
|
|
|
|
|
|
|
|
|
const contoPp1 = contaPresenzeGiocatore("pp1", eventi, presenze, OGGI);
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(
|
|
|
|
|
contoPp1,
|
|
|
|
|
oro,
|
|
|
|
|
"una risposta per evento, incluso un ritardo contato come presenza",
|
|
|
|
|
);
|
2026-09-08 14:33:51 +02:00
|
|
|
assert.equal(
|
|
|
|
|
statoBadge(presenzeDef, giocatoreAzzerato(contoPp1)).grado,
|
|
|
|
|
"oro",
|
2026-11-15 20:02:30 +01:00
|
|
|
`${oro} presenze: soglia oro raggiunta`,
|
2026-09-08 14:33:51 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const contoPp2 = contaPresenzeGiocatore("pp2", eventi, presenze, OGGI);
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(contoPp2, bronzo, "solo i presenti contano, l'assente su a-extra no");
|
2026-09-08 14:33:51 +02:00
|
|
|
const badgePp2 = statoBadge(presenzeDef, giocatoreAzzerato(contoPp2));
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(
|
|
|
|
|
badgePp2.grado,
|
|
|
|
|
"bronzo",
|
|
|
|
|
`${bronzo} presenze: soglia bronzo raggiunta, non oltre`,
|
|
|
|
|
);
|
2026-09-08 14:33:51 +02:00
|
|
|
assert.equal(badgePp2.prossimo, "argento");
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(badgePp2.prossimaSoglia, argento);
|
2026-09-08 14:33:51 +02:00
|
|
|
|
2026-11-15 20:02:30 +01:00
|
|
|
// Un pp2 a metà strada (`argento` presenze) sblocca l'argento.
|
|
|
|
|
for (let i = bronzo + 2; i <= argento + 1; i += 1) {
|
2026-09-08 14:33:51 +02:00
|
|
|
await rispondi(`${PREFISSO}-a${String(i).padStart(2, "0")}`, "pp2", "presente");
|
|
|
|
|
}
|
|
|
|
|
const eventiDopo = await leggiEventi();
|
|
|
|
|
const presenzeDopo = await leggiPresenze();
|
|
|
|
|
const contoPp2Dopo = contaPresenzeGiocatore("pp2", eventiDopo, presenzeDopo, OGGI);
|
2026-11-15 20:02:30 +01:00
|
|
|
assert.equal(contoPp2Dopo, argento, "bronzo + nuove presenze fino all'argento");
|
2026-09-08 14:33:51 +02:00
|
|
|
assert.equal(
|
|
|
|
|
statoBadge(presenzeDef, giocatoreAzzerato(contoPp2Dopo)).grado,
|
|
|
|
|
"argento",
|
2026-11-15 20:02:30 +01:00
|
|
|
`${argento} presenze: soglia argento raggiunta`,
|
2026-09-08 14:33:51 +02:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await prova(
|
|
|
|
|
"una risposta a un evento senza convocazione non conta per il badge, anche se la riga esiste",
|
|
|
|
|
async () => {
|
|
|
|
|
// "pp3" non è tra i convocati di questo evento (lista esplicita che lo esclude), ma
|
|
|
|
|
// scrive comunque una risposta "presente" — bypassando l'interfaccia, come farebbe
|
|
|
|
|
// chi parla direttamente con PostgREST. Deve restare a zero: la difesa è nella
|
|
|
|
|
// funzione pura, non in una policy RLS (a differenza di MVP/pagelle/badge social).
|
|
|
|
|
await creaEvento(`${PREFISSO}-ristretto`, 40, ["pp1"]);
|
|
|
|
|
await rispondi(`${PREFISSO}-ristretto`, "pp3", "presente");
|
|
|
|
|
|
|
|
|
|
const eventi = await leggiEventi();
|
|
|
|
|
const presenze = await leggiPresenze();
|
|
|
|
|
const conto = contaPresenzeGiocatore("pp3", eventi, presenze, OGGI);
|
|
|
|
|
assert.equal(conto, 0, "pp3 non era convocato: la risposta non conta");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
await rest(`risposte_presenze?evento_id=like.${PREFISSO}*`, { method: "DELETE" });
|
|
|
|
|
await rest(`eventi_app?id=like.${PREFISSO}*`, { method: "DELETE" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
riepilogo("presenze-badge");
|
|
|
|
|
}
|