TableTower/tests/test_book.gd
RobinFiredrake de942d57ee Book: the page editor reloads after undo and redo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 14:39:26 -06:00

121 lines
5.4 KiB
GDScript

extends TestBase
## The Book: pages saved from results, linked results following a page's edits,
## dropping a page onto a result or a card, unlinking, saving and undo.
## Run: godot --headless --path . -s res://tests/test_book.gd -- --sample
func _init() -> void:
if not await start_main():
return
main.set_mode(Main.Mode.BUILD)
await _frames()
await _test_book()
_test_pictures()
finish()
func _test_book() -> void:
var weapons := main.data.get_table("weapons")
var sword := weapons.entries[1]
var p := main.book.save_result(weapons, sword)
await _frames()
_check(main.book.visible and sword.page == p.id and p.text == sword.text, "Save to the Book makes a page and links the result")
main.book.editor.show_page(p)
p.text = "[b]The Sword of Holy Light[/b]"
p.details = "Glows near the undead."
main.book.editor._changed()
_check(sword.text == p.text and sword.details == p.details, "editing the page changes the linked result")
var loot := main.data.get_table("loot")
var target := loot.entries[0]
await _frames(2)
var row: Control = main.card_for("loot").rows[target.id]
_check(main.book.drop_page(p.id, row.get_global_rect().get_center()), "dropping a page on a result lands")
_check(target.page == p.id and target.text == p.text, "and links that result")
var n := loot.entries.size()
var card := main.card_for("loot")
main.book.drop_page(p.id, card.get_global_rect().position + Vector2(20, 12))
_check(loot.entries.size() == n + 1 and loot.entries[n].page == p.id, "dropping on the card's header adds a new linked result")
p.text = "Holy Sword"
main.book.editor._changed()
_check(target.text == "Holy Sword" and loot.entries[n].text == "Holy Sword", "one edit reaches every copy")
_check(main.data.page_uses(p.id) == 3, "the page counts its uses")
main.select_result("loot", target.id)
await _frames()
main.inspector.set_entry_text("A plain sword")
_check(target.page == "", "editing a result's own text unlinks it")
var d := main.data.to_dict()
var back := TowerBoard.new()
back.load_dict(d)
_check(back.book.size() == 1 and back.get_page(p.id).text == "Holy Sword", "the Book is saved in the spread")
main.book.list.clear()
main.book.refresh()
main.book._search.text = "nothing like it"
main.book.refresh()
_check(main.book.list.item_count == 0, "search filters pages")
main.book._search.text = ""
main.undo_redo._begin_edit()
main.data.remove_page(p.id)
main.mark_dirty()
main.undo_redo._end_edit()
_check(sword.page == "" and main.data.book.is_empty(), "deleting a page leaves its results as plain text")
main.undo_redo.undo()
await _frames()
_check(main.data.book.size() == 1, "undo brings the page back")
# Undo of a text edit shows in the open page editor at once.
var page := main.data.book[0]
main.book.editor.show_page(page)
var typed: TextEdit = main.book.editor.find_children("*", "TextEdit", true, false)[0]
typed.text = "Rusty Sword"
typed.text_changed.emit()
await _frames()
main.undo_redo.undo()
await _frames()
var shown: Array = main.book.editor.find_children("*", "TextEdit", true, false).map(func(edit: TextEdit) -> String: return edit.text)
_check(shown.has("Holy Sword"), "the page editor shows the undone text without reselecting: %s" % str(shown))
func _test_pictures() -> void:
var img := Image.create(900, 600, false, Image.FORMAT_RGBA8)
img.fill(Color.MEDIUM_PURPLE)
var r := BoardImages.from_image(img)
_check(r.has("id") and not r.has("error"), "a picture is stored")
main.data.images[r["id"]] = r["data"]
var tex := main.data.picture(r["id"])
_check(tex != null and maxi(tex.get_width(), tex.get_height()) == BoardImages.MAX_SIDE, "and shrunk to %d px" % BoardImages.MAX_SIDE)
main.data.images["img_unused"] = r["data"]
var e := main.data.get_table("weapons").entries[0]
e.image = r["id"]
var d := main.data.to_dict()
_check((d.get("images", {}) as Dictionary).has(r["id"]) and not (d["images"] as Dictionary).has("img_unused"), "only used pictures are saved")
var back := TowerBoard.new()
back.load_dict(d)
_check(back.picture(back.get_table("weapons").entries[0].image) != null, "pictures load with the spread")
var p := main.data.add_page("Pic page")
p.image = r["id"]
var e2 := main.data.get_table("weapons").entries[2]
main.data.link_page(e2, p.id)
_check(e2.image == r["id"], "a page's picture comes with it")
# Everything new survives an export and import of the spread file.
var t := main.data.get_table("weapons")
t.color = "#2f8a4f"
t.width = 300
t.roll_floor = "2"
e2.cost_value = "@gold"
e2.cost_amount = "3"
e2.add_pipe("loot")
e2.pipe_combine["loot"] = "max"
e2.pipe_pause["loot"] = true
e2.pipe_color["loot"] = "#c04a86"
var g := main.data.add_group("Outer", [])
var g2 := main.data.add_group("Inner", [])
g2.parent = g.id
var canon := JsonIO.canonical(JSON.parse_string(JSON.stringify(main.data.to_dict())))
var b2 := TowerBoard.new()
b2.load_dict(canon)
var t2 := b2.get_table("weapons")
var e3 := t2.entries[2]
_check(t2.color == "#2f8a4f" and t2.width == 300.0 and t2.roll_floor == "2", "card colour, size and limits import")
_check(e3.page == p.id and e3.image == r["id"] and e3.cost_amount == "3", "page links, pictures and costs import")
_check(e3.combine_for("loot") == "max" and e3.pipe_pause.has("loot") and e3.pipe_color.get("loot") == "#c04a86", "thread options import")
_check(b2.get_group(g2.id).parent == g.id and b2.book.size() == main.data.book.size() and b2.images.has(r["id"]), "nesting, the Book and pictures import")