Rimuove la serie «Conferme 24h» e i badge collegati
Toglie la serie dal profilo, dalla Home e dalle notifiche di traguardo, insieme ai badge «Risposta lampo» e «Mai un forfait» e al calcolo di serieConferme. La colonna risposto_il e il suo trigger restano nel database, inutilizzati. Test e documentazione allineati. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d0a8c7971
commit
bfb768fb5a
17 files changed
+69
-718
No files matched your search
@@ -1,204 +0,0 @@
|
||||
/**
|
||||
* Badge segreto Mai un forfait end-to-end contro il database locale:
|
||||
* `bun test/integration/s-mai-forfait-badge.test.ts`.
|
||||
*
|
||||
* I test unitari (`test/unit/presenze.test.ts`, `test/unit/badges.test.ts`) verificano
|
||||
* `serieConferme()`, `contaPresenzeGiocatore()` e `statoBadge()` come funzioni pure. Qui invece
|
||||
* si scrivono eventi con `creato_il` esplicito e risposte con `risposto_il` esplicito su
|
||||
* `eventi_app`/`risposte_presenze`, si rileggono via REST e si passa il risultato attraverso
|
||||
* entrambe le funzioni fino a `statoBadge()` sul badge segreto `s-mai-forfait`, che è l'unico a
|
||||
* combinare due statistiche indipendenti (serie di conferme rapide **e** presenze totali).
|
||||
*
|
||||
* Gira solo sullo stack locale (`npx supabase start`) e cancella le proprie righe alla fine:
|
||||
* usa id con il prefisso `test-s-mai-forfait-badge`, che nessun dato vero può avere.
|
||||
*/
|
||||
import assert from "node:assert/strict";
|
||||
import { badgeSegreti, statoBadge } from "@/lib/badges";
|
||||
import { serieConferme, 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";
|
||||
import type { Stato } from "@/lib/crapp-data";
|
||||
import type { MappaPresenze } from "@/lib/presenze";
|
||||
|
||||
const locale = statoLocale();
|
||||
|
||||
if (!locale) {
|
||||
salta("badge Mai un forfait sul database", "stack locale non attivo (npx supabase start)");
|
||||
riepilogo("s-mai-forfait-badge");
|
||||
} else {
|
||||
const { url: URL_BASE, servizio: SERVIZIO } = locale;
|
||||
console.log(`badge Mai un forfait su ${URL_BASE}`);
|
||||
|
||||
const PREFISSO = "test-s-mai-forfait-badge";
|
||||
const def = badgeSegreti.find((b) => b.id === "s-mai-forfait")!;
|
||||
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 ?? {}),
|
||||
},
|
||||
});
|
||||
|
||||
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) {
|
||||
const res = await rest("eventi_app", {
|
||||
method: "POST",
|
||||
headers: { Prefer: "resolution=merge-duplicates,return=representation" },
|
||||
body: JSON.stringify({
|
||||
id,
|
||||
tipo: "allenamento",
|
||||
titolo: `Evento ${id}`,
|
||||
data: dataEvento(i),
|
||||
creato_il: `${dataEvento(i)}T08:00:00Z`,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`creazione evento ${id}: ${res.status} ${await res.text()}`);
|
||||
}
|
||||
|
||||
async function rispondi(eventoId: string, giocatoreId: string, i: number) {
|
||||
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: "presente",
|
||||
risposto_il: `${dataEvento(i)}T08:30:00Z`,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`upsert su risposte_presenze: ${res.status} ${await res.text()}`);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async function leggiPresenzeETempi(): Promise<{
|
||||
presenze: MappaPresenze;
|
||||
tempi: Record<string, Record<string, string>>;
|
||||
}> {
|
||||
const res = await rest(
|
||||
`risposte_presenze?evento_id=like.${PREFISSO}*&select=evento_id,giocatore_id,stato,risposto_il`,
|
||||
);
|
||||
const righe = (await res.json()) as Array<{
|
||||
evento_id: string;
|
||||
giocatore_id: string;
|
||||
stato: Stato;
|
||||
risposto_il: string;
|
||||
}>;
|
||||
const presenze: MappaPresenze = {};
|
||||
const tempi: Record<string, Record<string, string>> = {};
|
||||
for (const r of righe) {
|
||||
(presenze[r.evento_id] ??= {})[r.giocatore_id] = r.stato;
|
||||
(tempi[r.evento_id] ??= {})[r.giocatore_id] = r.risposto_il;
|
||||
}
|
||||
return { presenze, tempi };
|
||||
}
|
||||
|
||||
function giocatoreAzzerato(serieConferme: number, presenze: number): Giocatore {
|
||||
return {
|
||||
...giocatori[0]!,
|
||||
serieConferme,
|
||||
presenze,
|
||||
mvp: 0,
|
||||
mediaVoto: 0,
|
||||
votiPagella: 0,
|
||||
palloni: 0,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await prova(
|
||||
"il segreto si sblocca solo quando entrambe le condizioni sono vere insieme",
|
||||
async () => {
|
||||
// "mf1": 15 eventi passati, presente e confermato in fretta a tutti — soddisfa
|
||||
// ampiamente sia la serie di conferme (>=10) sia le presenze (>=15).
|
||||
for (let i = 1; i <= 15; i += 1) {
|
||||
const id = `${PREFISSO}-a${String(i).padStart(2, "0")}`;
|
||||
await creaEvento(id, i);
|
||||
await rispondi(id, "mf1", i);
|
||||
}
|
||||
|
||||
const eventi15 = await leggiEventi();
|
||||
const { presenze: p15, tempi: t15 } = await leggiPresenzeETempi();
|
||||
|
||||
// Solo 9 eventi: serie di conferme appena sotto soglia, presenze sotto soglia.
|
||||
const eventi9 = eventi15.slice(0, 9);
|
||||
const serie9 = serieConferme("mf1", eventi9, t15, OGGI);
|
||||
const pres9 = contaPresenzeGiocatore("mf1", eventi9, p15, OGGI);
|
||||
assert.equal(serie9, 9);
|
||||
assert.equal(pres9, 9);
|
||||
assert.equal(
|
||||
statoBadge(def, giocatoreAzzerato(serie9, pres9)).grado,
|
||||
null,
|
||||
"9 conferme e 9 presenze: nessuna delle due soglie raggiunta",
|
||||
);
|
||||
|
||||
const serie15 = serieConferme("mf1", eventi15, t15, OGGI);
|
||||
const pres15 = contaPresenzeGiocatore("mf1", eventi15, p15, OGGI);
|
||||
assert.equal(serie15, 15);
|
||||
assert.equal(pres15, 15);
|
||||
assert.equal(
|
||||
statoBadge(def, giocatoreAzzerato(serie15, pres15)).grado,
|
||||
"oro",
|
||||
"serie conferme >=10 e presenze >=15: il segreto si sblocca",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
await prova("una risposta lenta rompe la serie ma non le presenze già accumulate", async () => {
|
||||
// Un sedicesimo evento con risposta arrivata oltre le 24h: la serie conferme torna a 0,
|
||||
// ma le presenze (indipendenti) restano a 16 — il segreto deve richiudersi, non serve
|
||||
// ripartire da zero anche sulle presenze.
|
||||
const idLento = `${PREFISSO}-a16`;
|
||||
await creaEvento(idLento, 16);
|
||||
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: idLento,
|
||||
giocatore_id: "mf1",
|
||||
stato: "presente",
|
||||
risposto_il: `${dataEvento(18)}T08:00:00Z`, // 2 giorni dopo la convocazione
|
||||
}),
|
||||
});
|
||||
if (!res.ok)
|
||||
throw new Error(`upsert su risposte_presenze: ${res.status} ${await res.text()}`);
|
||||
|
||||
const eventi = await leggiEventi();
|
||||
const { presenze, tempi } = await leggiPresenzeETempi();
|
||||
const serie = serieConferme("mf1", eventi, tempi, OGGI);
|
||||
const conto = contaPresenzeGiocatore("mf1", eventi, presenze, OGGI);
|
||||
assert.equal(serie, 0, "la risposta lenta azzera la serie di conferme");
|
||||
assert.equal(conto, 16, "le presenze restano quelle di sempre, indipendenti dalla serie");
|
||||
assert.equal(
|
||||
statoBadge(def, giocatoreAzzerato(serie, conto)).grado,
|
||||
null,
|
||||
"presenze abbondanti ma serie azzerata: il segreto si richiude",
|
||||
);
|
||||
});
|
||||
} finally {
|
||||
await rest(`risposte_presenze?evento_id=like.${PREFISSO}*`, { method: "DELETE" });
|
||||
await rest(`eventi_app?id=like.${PREFISSO}*`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
riepilogo("s-mai-forfait-badge");
|
||||
}
|
||||
@@ -254,8 +254,8 @@ if (!locale) {
|
||||
|
||||
// --- presenze: la risposta si cambia, il cronometro no --------------------------
|
||||
// Due colonne che sembrano la stessa cosa e non lo sono: `risposto_il` è la PRIMA
|
||||
// risposta e alimenta la serie "Conferme 24h", `aggiornato_il` è l'ultima modifica e non
|
||||
// alimenta niente. Il trigger `risposte_presenze_risposto_il_immutabile` (M9) tiene ferma
|
||||
// risposta, `aggiornato_il` è l'ultima modifica; oggi nessuna statistica le usa (la serie
|
||||
// «Conferme 24h» è stata rimossa). Il trigger `risposte_presenze_risposto_il_immutabile` (M9) tiene ferma
|
||||
// la prima: senza, chi risponde subito e ci ripensa una settimana dopo risulterebbe lento.
|
||||
await prova(
|
||||
"la risposta di presenza si aggiorna senza far ripartire il cronometro",
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
/**
|
||||
* Badge Risposta lampo end-to-end contro il database locale:
|
||||
* `bun test/integration/serie-conferme-badge.test.ts`.
|
||||
*
|
||||
* I test unitari (`test/unit/presenze.test.ts`, `test/unit/badges.test.ts`) verificano
|
||||
* `serieConferme()` e `statoBadge()` come funzioni pure, con eventi e risposte costruiti a
|
||||
* mano. Qui invece si scrivono allenamenti/partite ed eventi veri su `eventi_app`, e risposte
|
||||
* vere su `risposte_presenze` con `creato_il`/`risposto_il` espliciti, si rileggono via REST
|
||||
* con la stessa forma di `daRiga()`/`fetchPresenze()`, e si passa il risultato attraverso
|
||||
* `serieConferme()` fino a `statoBadge()` sul badge `serie-conferme`: se una colonna cambia
|
||||
* nome, o la mappatura `creato_il`/`risposto_il` -> `creatoIl`/`tempi` si rompe, qui il grado
|
||||
* del badge torna sbagliato anche se i test unitari restano verdi, perché quelli non toccano
|
||||
* mai il database.
|
||||
*
|
||||
* Copre due comportamenti non ovvi con dati veri, non solo con numeri scelti a mano:
|
||||
* - una risposta arrivata oltre le 24h dalla convocazione azzera la serie, anche dopo una
|
||||
* striscia lunga;
|
||||
* - partite e allenamenti contano nella stessa serie, senza bisogno di un filtro per tipo.
|
||||
*
|
||||
* Gira solo sullo stack locale (`npx supabase start`) e cancella le proprie righe alla fine:
|
||||
* usa id con il prefisso `test-serie-conferme-badge`, che nessun dato vero può avere.
|
||||
*/
|
||||
import assert from "node:assert/strict";
|
||||
import { badgeDefs, statoBadge } from "@/lib/badges";
|
||||
import { serieConferme } 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";
|
||||
|
||||
const locale = statoLocale();
|
||||
|
||||
if (!locale) {
|
||||
salta("badge Risposta lampo sul database", "stack locale non attivo (npx supabase start)");
|
||||
riepilogo("serie-conferme-badge");
|
||||
} else {
|
||||
const { url: URL_BASE, servizio: SERVIZIO } = locale;
|
||||
console.log(`badge Risposta lampo su ${URL_BASE}`);
|
||||
|
||||
const PREFISSO = "test-serie-conferme-badge";
|
||||
const serieDef = badgeDefs.find((b) => b.id === "serie-conferme")!;
|
||||
// "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);
|
||||
};
|
||||
|
||||
/** Istante di convocazione: le 8 del mattino del giorno dell'evento. */
|
||||
const creazioneEvento = (i: number) => `${dataEvento(i)}T08:00:00Z`;
|
||||
|
||||
async function creaEvento(
|
||||
id: string,
|
||||
i: number,
|
||||
tipo: "allenamento" | "partita" = "allenamento",
|
||||
) {
|
||||
const res = await rest("eventi_app", {
|
||||
method: "POST",
|
||||
headers: { Prefer: "resolution=merge-duplicates,return=representation" },
|
||||
body: JSON.stringify({
|
||||
id,
|
||||
tipo,
|
||||
titolo: `Evento ${id}`,
|
||||
data: dataEvento(i),
|
||||
creato_il: creazioneEvento(i),
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`creazione evento ${id}: ${res.status} ${await res.text()}`);
|
||||
}
|
||||
|
||||
async function rispondi(
|
||||
eventoId: string,
|
||||
giocatoreId: string,
|
||||
stato: string,
|
||||
rispostoIl: 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,
|
||||
risposto_il: rispostoIl,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(`upsert su risposte_presenze: ${res.status} ${await res.text()}`);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/** Stessa forma di `fetchPresenze()`: eventoId -> giocatoreId -> istante della risposta. */
|
||||
async function leggiTempi(): Promise<Record<string, Record<string, string>>> {
|
||||
const res = await rest(
|
||||
`risposte_presenze?evento_id=like.${PREFISSO}*&select=evento_id,giocatore_id,risposto_il`,
|
||||
);
|
||||
const righe = (await res.json()) as Array<{
|
||||
evento_id: string;
|
||||
giocatore_id: string;
|
||||
risposto_il: string;
|
||||
}>;
|
||||
const mappa: Record<string, Record<string, string>> = {};
|
||||
for (const r of righe) (mappa[r.evento_id] ??= {})[r.giocatore_id] = r.risposto_il;
|
||||
return mappa;
|
||||
}
|
||||
|
||||
function giocatoreAzzerato(serieConferme: number): Giocatore {
|
||||
return {
|
||||
...giocatori[0]!,
|
||||
serieConferme,
|
||||
mvp: 0,
|
||||
mediaVoto: 0,
|
||||
votiPagella: 0,
|
||||
palloni: 0,
|
||||
presenze: 0,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await prova(
|
||||
"il badge passa da bronzo a oro con conferme rapide vere, e una risposta lenta lo azzera",
|
||||
async () => {
|
||||
// 15 eventi passati, tutti confermati mezz'ora dopo la convocazione per "sc1":
|
||||
// bronzo (3), poi argento (8), poi oro (15) man mano che si rileggono i dati.
|
||||
for (let i = 1; i <= 15; i += 1) {
|
||||
const id = `${PREFISSO}-a${String(i).padStart(2, "0")}`;
|
||||
await creaEvento(id, i);
|
||||
await rispondi(id, "sc1", "presente", `${dataEvento(i)}T08:30:00Z`);
|
||||
}
|
||||
const eventi15 = await leggiEventi();
|
||||
const tempi15 = await leggiTempi();
|
||||
|
||||
const serieA3 = serieConferme("sc1", eventi15.slice(0, 3), tempi15, OGGI);
|
||||
assert.equal(serieA3, 3);
|
||||
assert.equal(
|
||||
statoBadge(serieDef, giocatoreAzzerato(serieA3)).grado,
|
||||
"bronzo",
|
||||
"3 conferme rapide di fila: soglia bronzo",
|
||||
);
|
||||
|
||||
const serieA8 = serieConferme("sc1", eventi15.slice(0, 8), tempi15, OGGI);
|
||||
assert.equal(serieA8, 8);
|
||||
assert.equal(
|
||||
statoBadge(serieDef, giocatoreAzzerato(serieA8)).grado,
|
||||
"argento",
|
||||
"8 conferme rapide di fila: soglia argento",
|
||||
);
|
||||
|
||||
const serieCompleta = serieConferme("sc1", eventi15, tempi15, OGGI);
|
||||
assert.equal(serieCompleta, 15, "confermato in fretta a tutti e 15, nessun buco");
|
||||
assert.equal(
|
||||
statoBadge(serieDef, giocatoreAzzerato(serieCompleta)).grado,
|
||||
"oro",
|
||||
"15 conferme rapide di fila: soglia oro",
|
||||
);
|
||||
|
||||
// Un sedicesimo evento con una risposta arrivata dopo 2 giorni azzera la serie.
|
||||
const idLento = `${PREFISSO}-a16`;
|
||||
await creaEvento(idLento, 16);
|
||||
await rispondi(idLento, "sc1", "presente", `${dataEvento(18)}T08:00:00Z`);
|
||||
const eventiConRitardo = await leggiEventi();
|
||||
const tempiConRitardo = await leggiTempi();
|
||||
const serieDopoRitardo = serieConferme("sc1", eventiConRitardo, tempiConRitardo, OGGI);
|
||||
assert.equal(
|
||||
serieDopoRitardo,
|
||||
0,
|
||||
"una risposta oltre le 24h azzera la serie, anche dopo 15 conferme di fila",
|
||||
);
|
||||
assert.equal(
|
||||
statoBadge(serieDef, giocatoreAzzerato(serieDopoRitardo)).grado,
|
||||
null,
|
||||
"serie azzerata: nessun grado, si riparte da zero",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
await prova(
|
||||
"partite e allenamenti contano nella stessa serie, senza filtro per tipo",
|
||||
async () => {
|
||||
// "sc2": conferma rapida su un allenamento, poi su una partita, poi su un altro
|
||||
// allenamento: la serie sale a 3 senza bisogno che siano tutti dello stesso tipo.
|
||||
await creaEvento(`${PREFISSO}-b01`, 30, "allenamento");
|
||||
await creaEvento(`${PREFISSO}-b02`, 31, "partita");
|
||||
await creaEvento(`${PREFISSO}-b03`, 32, "allenamento");
|
||||
await rispondi(`${PREFISSO}-b01`, "sc2", "presente", `${dataEvento(30)}T08:30:00Z`);
|
||||
await rispondi(`${PREFISSO}-b02`, "sc2", "presente", `${dataEvento(31)}T08:30:00Z`);
|
||||
await rispondi(`${PREFISSO}-b03`, "sc2", "presente", `${dataEvento(32)}T08:30:00Z`);
|
||||
|
||||
const eventiLetti = await leggiEventi();
|
||||
const tempiLetti = await leggiTempi();
|
||||
const serie = serieConferme("sc2", eventiLetti, tempiLetti, OGGI);
|
||||
assert.equal(serie, 3, "partita in mezzo a due allenamenti: la serie resta a 3");
|
||||
assert.equal(
|
||||
statoBadge(serieDef, giocatoreAzzerato(serie)).grado,
|
||||
"bronzo",
|
||||
"3 conferme rapide di fila, tipo misto: soglia bronzo",
|
||||
);
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await rest(`risposte_presenze?evento_id=like.${PREFISSO}*`, { method: "DELETE" });
|
||||
await rest(`eventi_app?id=like.${PREFISSO}*`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
riepilogo("serie-conferme-badge");
|
||||
}
|
||||
@@ -34,7 +34,6 @@ function g(valori: Partial<Giocatore> = {}): Giocatore {
|
||||
streak: 0,
|
||||
serieAllenamenti: 0,
|
||||
seriePartite: 0,
|
||||
serieConferme: 0,
|
||||
...valori,
|
||||
};
|
||||
}
|
||||
@@ -146,16 +145,6 @@ assert.equal(gradoRaggiunto(serieAllenamentiDef, 9), "argento");
|
||||
assert.equal(gradoRaggiunto(serieAllenamentiDef, 10), "oro");
|
||||
assert.equal(gradoRaggiunto(serieAllenamentiDef, 50), "oro", "oltre l'oro resta oro");
|
||||
|
||||
// --- serie-conferme: soglie 3 / 8 / 15 ----------------------------------------
|
||||
const serieConfermeDef = badgeDefs.find((b) => b.id === "serie-conferme")!;
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 2), null, "sotto la prima soglia nessun grado");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 3), "bronzo", "la soglia è inclusiva");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 7), "bronzo");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 8), "argento");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 14), "argento");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 15), "oro");
|
||||
assert.equal(gradoRaggiunto(serieConfermeDef, 50), "oro", "oltre l'oro resta oro");
|
||||
|
||||
// --- badgeGiocatore ----------------------------------------------------------
|
||||
assert.equal(badgeGiocatore(g()).length, badgeDefs.length, "i badge normali sono sempre tutti");
|
||||
assert.ok(
|
||||
@@ -200,36 +189,6 @@ assert.deepEqual(
|
||||
["s-cacche"],
|
||||
);
|
||||
assert.equal(badgeSegretiSbloccati(g({ cacche: 2 })).length, 0, "2 cacche non bastano");
|
||||
assert.deepEqual(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 10, presenze: 15 })).map((b) => b.def.id),
|
||||
["s-mai-forfait"],
|
||||
);
|
||||
assert.equal(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 10, presenze: 14 })).length,
|
||||
0,
|
||||
"servono entrambe le condizioni",
|
||||
);
|
||||
// Confini isolati: ogni soglia testata da sola, con l'altra abbondantemente sopra.
|
||||
assert.equal(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 9, presenze: 30 })).length,
|
||||
0,
|
||||
"serieConferme appena sotto soglia, presenze abbondanti: non basta",
|
||||
);
|
||||
assert.deepEqual(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 10, presenze: 30 })).map((b) => b.def.id),
|
||||
["s-mai-forfait"],
|
||||
"serieConferme esattamente al confine, presenze abbondanti: sblocca",
|
||||
);
|
||||
assert.equal(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 30, presenze: 14 })).length,
|
||||
0,
|
||||
"presenze appena sotto soglia, serieConferme abbondante: non basta",
|
||||
);
|
||||
assert.deepEqual(
|
||||
badgeSegretiSbloccati(g({ serieConferme: 30, presenze: 15 })).map((b) => b.def.id),
|
||||
["s-mai-forfait"],
|
||||
"presenze esattamente al confine, serieConferme abbondante: sblocca",
|
||||
);
|
||||
|
||||
// --- collezioneBadge ---------------------------------------------------------
|
||||
const vuota = collezioneBadge(g());
|
||||
@@ -262,7 +221,6 @@ assert.equal(
|
||||
palloni: 10,
|
||||
presenze: 30,
|
||||
serieAllenamenti: 10,
|
||||
serieConferme: 15,
|
||||
}),
|
||||
),
|
||||
null,
|
||||
|
||||
@@ -19,7 +19,6 @@ function g(valori: Partial<Giocatore> = {}): Giocatore {
|
||||
streak: 0,
|
||||
serieAllenamenti: 0,
|
||||
seriePartite: 0,
|
||||
serieConferme: 0,
|
||||
...valori,
|
||||
};
|
||||
}
|
||||
@@ -57,7 +56,7 @@ assert.ok(
|
||||
// --- serie: solo sul traguardo esatto ----------------------------------------
|
||||
assert.ok(ids(g({ serieAllenamenti: 3 })).includes("serie:allenamenti:3"));
|
||||
assert.ok(!ids(g({ serieAllenamenti: 4 })).includes("serie:allenamenti:4"), "4 non è un traguardo");
|
||||
assert.ok(ids(g({ serieConferme: 8 })).includes("serie:conferme:8"));
|
||||
assert.ok(ids(g({ seriePartite: 5 })).includes("serie:partite:5"));
|
||||
|
||||
// --- obiettivi di squadra: uno solo, tra il 90% e il 99% --------------------
|
||||
const obiettivo = (id: string, valore: number): ObiettivoSquadra => ({
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
contaPresenzeGiocatore,
|
||||
destinatariSollecito,
|
||||
includeInfortunato,
|
||||
serieConferme,
|
||||
serieConsecutiva,
|
||||
testoSollecito,
|
||||
totaliEventiGiocatore,
|
||||
@@ -218,91 +217,9 @@ assert.ok(
|
||||
"senza luogo la riga manca",
|
||||
);
|
||||
|
||||
// --- serie conferme: risposta entro 24h dalla convocazione --------------------
|
||||
const convocati = (id: string, data: string, creatoIl?: string): Evento => ({
|
||||
...ev(id, "allenamento", data),
|
||||
...(creatoIl === undefined ? {} : { creatoIl }),
|
||||
});
|
||||
|
||||
const conConvocazione: Evento[] = [
|
||||
convocati("c1", "2026-08-06", "2026-08-01T10:00:00Z"),
|
||||
convocati("c2", "2026-08-13", "2026-08-08T10:00:00Z"),
|
||||
convocati("c3", "2026-08-20", "2026-08-15T10:00:00Z"),
|
||||
convocati("c4", "2026-08-27"), // evento generato dal client: nessuna convocazione tracciata
|
||||
convocati("c5", "2026-09-10", "2026-09-05T10:00:00Z"), // futuro
|
||||
];
|
||||
|
||||
const tempi: MappaTempiRisposta = {
|
||||
c1: { g1: "2026-08-01T11:00:00Z" }, // un'ora dopo
|
||||
c2: { g1: "2026-08-10T10:00:00Z" }, // due giorni dopo: buco
|
||||
c3: { g1: "2026-08-16T09:59:00Z" }, // appena dentro le 24h
|
||||
c5: { g1: "2026-09-05T10:30:00Z" },
|
||||
};
|
||||
|
||||
assert.equal(serieConferme("g1", conConvocazione, tempi, OGGI), 1, "il ritardo su c2 azzera");
|
||||
assert.equal(
|
||||
serieConferme("g1", conConvocazione.slice(0, 1), tempi, OGGI),
|
||||
1,
|
||||
"una risposta rapida vale 1",
|
||||
);
|
||||
assert.equal(
|
||||
serieConferme("g1", [conConvocazione[0]!, conConvocazione[3]!, conConvocazione[2]!], tempi, OGGI),
|
||||
2,
|
||||
"un evento senza istante di convocazione viene saltato, non spezza la serie",
|
||||
);
|
||||
assert.equal(serieConferme("g2", conConvocazione, tempi, OGGI), 0, "chi non risponde è a zero");
|
||||
|
||||
// Chi non è convocato non spezza la serie conferme di nessun altro (stesso filtro condiviso
|
||||
// con `serieConsecutiva`, vedi sopra).
|
||||
const c6Ristretto: Evento = {
|
||||
...convocati("c6", "2026-08-08", "2026-08-03T10:00:00Z"),
|
||||
convocati: ["g9"],
|
||||
};
|
||||
assert.equal(
|
||||
serieConferme("g1", [conConvocazione[0]!, c6Ristretto], tempi, OGGI),
|
||||
1,
|
||||
"un evento convocato solo per un altro giocatore resta fuori, la serie di g1 non ne risente",
|
||||
);
|
||||
|
||||
// Le conferme contano partite e allenamenti insieme: nessuna versione per tipo.
|
||||
const mistiTipo: Evento[] = [
|
||||
convocati("m1", "2026-08-06", "2026-08-01T10:00:00Z"),
|
||||
{ ...convocati("m2", "2026-08-13", "2026-08-08T10:00:00Z"), tipo: "partita" },
|
||||
];
|
||||
const tempiMisti: MappaTempiRisposta = {
|
||||
m1: { g1: "2026-08-01T11:00:00Z" }, // un'ora dopo
|
||||
m2: { g1: "2026-08-08T11:00:00Z" }, // un'ora dopo, ma è una partita
|
||||
};
|
||||
assert.equal(
|
||||
serieConferme("g1", mistiTipo, tempiMisti, OGGI),
|
||||
2,
|
||||
"partite e allenamenti si sommano nella stessa serie",
|
||||
);
|
||||
|
||||
// Il confronto con le 24h è inclusivo: esattamente al bordo conta, un secondo oltre azzera.
|
||||
const bordo: Evento[] = [convocati("b1", "2026-08-06", "2026-08-01T10:00:00Z")];
|
||||
assert.equal(
|
||||
serieConferme("g1", bordo, { b1: { g1: "2026-08-02T10:00:00Z" } }, OGGI),
|
||||
1,
|
||||
"esattamente 24h dopo: il confronto è <=, quindi conta",
|
||||
);
|
||||
assert.equal(
|
||||
serieConferme("g1", bordo, { b1: { g1: "2026-08-02T10:00:01Z" } }, OGGI),
|
||||
0,
|
||||
"un secondo oltre le 24h azzera",
|
||||
);
|
||||
|
||||
// Un evento futuro non conta ancora, anche con una risposta rapidissima già registrata.
|
||||
const futuro: Evento[] = [convocati("f1", "2026-09-10", "2026-09-05T10:00:00Z")];
|
||||
assert.equal(
|
||||
serieConferme("g1", futuro, { f1: { g1: "2026-09-05T10:05:00Z" } }, OGGI),
|
||||
0,
|
||||
"l'evento di domani non è ancora passato, non entra nel calcolo",
|
||||
);
|
||||
|
||||
// --- cache locale dopo una risposta: il cronometro non riparte ----------------
|
||||
// Stessa regola del database: `risposto_il` è la PRIMA risposta e un trigger la congela.
|
||||
// Qui la cache deve imitarla, altrimenti la serie "Conferme 24h" mente fino al refresh.
|
||||
// Qui la cache deve imitarla, altrimenti la cache mente fino al refresh.
|
||||
const PRIMA = "2026-08-01T10:00:00Z";
|
||||
const POI = "2026-08-08T10:00:00Z";
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { giocatori, type Giocatore } from "@/lib/crapp-data";
|
||||
import { aggiornaSerie, serieDefs, serieGiocatore, serieMigliore, statoSerie } from "@/lib/serie";
|
||||
|
||||
function g(valori: Partial<Giocatore> = {}): Giocatore {
|
||||
return { ...giocatori[0]!, serieAllenamenti: 0, seriePartite: 0, serieConferme: 0, ...valori };
|
||||
return { ...giocatori[0]!, serieAllenamenti: 0, seriePartite: 0, ...valori };
|
||||
}
|
||||
|
||||
const allenamenti = serieDefs.find((d) => d.tipo === "allenamenti")!;
|
||||
@@ -45,16 +45,16 @@ assert.deepEqual([fuoriScala.prossimo, fuoriScala.manca, fuoriScala.progresso],
|
||||
assert.equal(fuoriScala.messaggio, "Serie leggendaria: sei fuori scala!");
|
||||
|
||||
// --- serieGiocatore / serieMigliore ------------------------------------------
|
||||
const tutte = serieGiocatore(g({ serieAllenamenti: 4, seriePartite: 1, serieConferme: 9 }));
|
||||
assert.equal(tutte.length, serieDefs.length, "tre serie indipendenti");
|
||||
const tutte = serieGiocatore(g({ serieAllenamenti: 4, seriePartite: 6 }));
|
||||
assert.equal(tutte.length, serieDefs.length, "due serie indipendenti");
|
||||
assert.deepEqual(
|
||||
tutte.map((s) => s.valore),
|
||||
[4, 1, 9],
|
||||
[4, 6],
|
||||
"ogni serie legge il proprio contatore",
|
||||
);
|
||||
|
||||
const migliore = serieMigliore(g({ serieAllenamenti: 4, seriePartite: 1, serieConferme: 9 }));
|
||||
assert.equal(migliore.def.tipo, "conferme", "in home si mostra la serie più lunga");
|
||||
const migliore = serieMigliore(g({ serieAllenamenti: 4, seriePartite: 6 }));
|
||||
assert.equal(migliore.def.tipo, "partite", "in home si mostra la serie più lunga");
|
||||
assert.ok(serieMigliore(g()), "anche a zero c'è sempre una serie da mostrare");
|
||||
|
||||
// --- invarianti sulle definizioni --------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user