---
type: "Source File"
title: "\"\"\"Authoritative server-side scoring for the public CopeCheck quiz."
description: "\"\"\"Authoritative server-side scoring for the public CopeCheck quiz. The browser renders these choices, but submitted point values and cope labels are untrusted. Only question/answer indices cross the trust boundary."
resource: "https://quiz.copecheck.com"
tags: ["copecheck", "quiz", "source-file", "py"]
timestamp: "2026-08-24T14:06:02Z"
site: "quiz"
source_path: "copecheck-quiz/quiz_integrity.py"
---

# """Authoritative server-side scoring for the public CopeCheck quiz.

Source path: `copecheck-quiz/quiz_integrity.py`  
Index snapshot generated: `2026-11-24T13:27:52Z`  
File modified: `2026-08-24T14:06:02Z`

```py
"""Authoritative server-side scoring for the public CopeCheck quiz.

The browser renders these choices, but submitted point values and cope labels
are untrusted. Only question/answer indices cross the trust boundary.
"""
from __future__ import annotations

from collections import Counter
from typing import Any


# Each inner tuple is (points, cope_type), in the same display order as
# static/js/quiz.js. Tests verify the two copies stay aligned.
ANSWER_KEY: tuple[tuple[tuple[int, str], ...], ...] = (
    ((18,"historical_analogy_cope"),(12,"gradualism_cope"),(8,"partial_acknowledgment"),(3,"somewhat_lucid"),(0,"reality")),
    ((18,"sector_exceptionalism"),(12,"copilot_fallacy"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"copilot_fallacy"),(13,"productivity_gospel"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"regulatory_hopium"),(12,"regulatory_hopium"),(7,"partial_acknowledgment"),(3,"somewhat_lucid"),(0,"reality")),
    ((18,"wishful_thinking"),(12,"human_premium_cope"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"gradualism_cope"),(12,"gradualism_cope"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"retraining_fantasy"),(12,"retraining_fantasy"),(7,"partial_acknowledgment"),(3,"somewhat_lucid"),(0,"reality")),
    ((18,"oversight_cope"),(12,"oversight_cope"),(6,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((16,"partial_acknowledgment"),(12,"partial_acknowledgment"),(5,"somewhat_lucid"),(2,"somewhat_lucid"),(0,"reality")),
    ((20,"pure_cope"),(15,"heavy_cope"),(10,"partial_cope"),(4,"somewhat_lucid"),(0,"reality")),
    ((18,"historical_analogy_cope"),(12,"historical_analogy_cope"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"human_premium_cope"),(12,"human_premium_cope"),(6,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"empathy_shield"),(12,"empathy_shield"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"sector_exceptionalism"),(14,"regulatory_hopium"),(4,"somewhat_lucid"),(0,"reality"),(0,"reality")),
    ((20,"pure_cope"),(14,"copilot_fallacy"),(10,"retraining_fantasy"),(3,"somewhat_lucid"),(0,"reality")),
    ((18,"productivity_gospel"),(12,"gradualism_cope"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((16,"copilot_fallacy"),(10,"partial_acknowledgment"),(6,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"historical_analogy_cope"),(12,"historical_analogy_cope"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
    ((18,"pure_cope"),(12,"heavy_cope"),(6,"partial_cope"),(2,"somewhat_lucid"),(0,"reality")),
    ((20,"productivity_gospel"),(14,"copilot_fallacy"),(7,"partial_acknowledgment"),(2,"somewhat_lucid"),(0,"reality")),
)


class InvalidSubmission(ValueError):
    pass


def _short_optional(value: Any, limit: int) -> str | None:
    if value is None:
        return None
    cleaned = " ".join(str(value).split())[:limit]
    return cleaned or None


def _bounded_int(value: Any, minimum: int, maximum: int) -> int | None:
    return value if type(value) is int and minimum <= value <= maximum else None


def validated_submission(raw: Any) -> dict[str, Any]:
    if not isinstance(raw, dict) or not isinstance(raw.get("answers"), list):
        raise InvalidSubmission("Missing answers")
    submitted = raw["answers"]
    if len(submitted) != len(ANSWER_KEY):
        raise InvalidSubmission(f"Exactly {len(ANSWER_KEY)} answers are required")

    by_question: dict[int, int] = {}
    for item in submitted:
        if not isinstance(item, dict):
            raise InvalidSubmission("Each answer must be an object")
        question = item.get("question_num")
        answer = item.get("answer_index")
        if type(question) is not int or type(answer) is not int:
            raise InvalidSubmission("Question and answer indices must be integers")
        if question < 1 or question > len(ANSWER_KEY) or question in by_question:
            raise InvalidSubmission("Questions must appear exactly once")
        if answer < 0 or answer >= len(ANSWER_KEY[question - 1]):
            raise InvalidSubmission("Answer index is out of range")
        by_question[question] = answer

    answers = []
    types: Counter[str] = Counter()
    total_points = 0
    for question in range(1, len(ANSWER_KEY) + 1):
        answer_index = by_question[question]
        points, cope_type = ANSWER_KEY[question - 1][answer_index]
        answers.append({
            "question_num": question,
            "answer_index": answer_index,
            "cope_points": points,
            "cope_type": cope_type,
        })
        total_points += points
        types[cope_type] += 1

    score = min(total_points / 4, 100)
    primary_type = max(types, key=types.get)
    return {
        "answers": answers,
        "cope_score": score,
        "primary_cope_type": primary_type,
        "started_at": _short_optional(raw.get("started_at"), 40),
        "screwed_job": _short_optional(raw.get("screwed_job"), 160),
        "screwed_industry": _short_optional(raw.get("screwed_industry"), 160),
        "screwed_experience": _short_optional(raw.get("screwed_experience"), 40),
        "screwed_computer_pct": _bounded_int(raw.get("screwed_computer_pct"), 0, 100),
        "screwed_worry": _bounded_int(raw.get("screwed_worry"), 0, 10),
        "comments_text": _short_optional(raw.get("comments_text"), 2000),
    }
```
