"""long_text.py - what does Paperclip do to a long text kept in a task?

    python long_text.py

paper_agent.py keeps a whole paper in a task's description. Two things
decided how it does that, and this check shows both.
"""
import json

import scratch

LINE = "A sentence of the paper, with ten words in it. "


def main():
    made, boss = scratch.company("Check long text")
    tasks_path = "/companies/" + made["id"] + "/issues"

    long_text = LINE * 100
    task = scratch.send("POST", tasks_path,
                        {"title": "A long text", "description": long_text})
    alone = scratch.send("GET", "/issues/" + task["id"])
    listed = scratch.send("GET", tasks_path)[0]
    print(f"Sent: {len(long_text):,} characters")
    print(f"The task asked for alone: {len(alone['description']):,} characters")
    print(f"The same task in the list of tasks: {len(listed['description']):,} characters")

    as_json = json.dumps({"paper": "Line one.\nLine two."})
    task = scratch.send("POST", tasks_path, {"title": "JSON", "description": as_json})
    back = scratch.send("GET", "/issues/" + task["id"])["description"]
    print("JSON sent:     ", repr(as_json))
    print("JSON came back:", repr(back))
    try:
        json.loads(back)
        print("Python can read it back as JSON.")
    except ValueError as error:
        print("Python cannot read it back as JSON:", error)
    scratch.remove(made)


if __name__ == "__main__":
    main()
