Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / tests / editor / script_edit_tests.luc

## The Script node's second round: Time Dependent inferred from the code and
## overridden by its row; an unchanged input's file kept between cooks; the
## check, completion and hover while typing; the disk cache kept to its limit; and packages resolved
## as the editor's own build resolved them (a registry-named package in a
## workspace of checkouts takes the checkout, so luce-std comes in once).
from luce_std import files, paths, process, math
from workspace import Workspace
import node_commands
import script_node
import script_cook
import script_cache
import timeline_commands
import code_cook
import thread
from script_check import ScriptChecker
from luce_geocore.geocore import ScriptHelp
from script_packages import editor_packages, dependencies
import time

## Script builds find luce-geocore and the packages beside this checkout, in a
## workspace of checkouts; a clone from the registry has none beside it, and
## its scripts take every package from the registry, as a released editor's do.
## Whether there are checkouts.
func use_checkouts() -> bool!:
    let here = process.variable("LUC_TEST_DIR") else trap("LUC_TEST_DIR")
    let root = paths.directory(paths.directory(paths.directory(here)))
    let geocore = paths.join(root, "luce-geocore")
    if not files.exists(paths.join(geocore, "package.prisma")):
        process.unset_variable("LUCED_GEOCORE")
        return false
    process.set_variable("LUCED_GEOCORE", geocore)
    return true

func top(w: Workspace) -> float!:
    return (w.geometry else trap("geometry")).bounds().maximum.y

pub func script_edit_tests() -> unit!:
    let checkouts = use_checkouts()
    time_tests()
    input_tests()
    check_tests()
    registry_check_tests()
    failing_check_tests()
    help_tests()
    cache_tests()
    editor_set_tests()
    package_tests(checkouts)

## Auto follows the timeline when the code reads it; On and Off override.
func time_tests() -> unit!:
    let w = Workspace()
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Grid", 0.0, 0.0)
    node_commands.add_node(w, "Script", 0.0, 100.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    let node = w.network.require(id)
    assert(not node.time_dependent)
    script_node.set_code(w, id, "from luce_geocore.script import Cook\n\npub func cook(k: Cook*) -> !:\n    let positions = try k.geometry().positions()\n    for i in 0..<positions.length:\n        positions[i].y = (f32)k.frame()\n")
    assert(node.failure == "", node.failure)
    assert(node.time_dependent)
    timeline_commands.set_frame(w, 5.0)
    assert(math.abs(top(w) - 5.0) < 1e-5)
    # Off: the frame no longer recooks it.
    w.network.selected = id
    node_commands.set_parameter(w, script_cook.time_slot, 2.0)
    assert(not node.time_dependent)
    timeline_commands.set_frame(w, 9.0)
    assert(math.abs(top(w) - 5.0) < 1e-5)
    # On, for code that reads the time some way the scan cannot see.
    node_commands.set_parameter(w, script_cook.time_slot, 1.0)
    assert(node.time_dependent)
    timeline_commands.set_frame(w, 10.0)
    assert(math.abs(top(w) - 10.0) < 1e-5)
    # Auto again, with code that does not read it.
    node_commands.set_parameter(w, script_cook.time_slot, 0.0)
    script_node.set_code(w, id, "from luce_geocore.script import Cook\n\npub func cook(k: Cook*) -> !:\n    k.warning(\"still\")\n")
    assert(not node.time_dependent)
    (w.network.sources else trap("sources")).scripts.close()
    print("PASS script Time Dependent: inferred from k.frame(), overridden On and Off")

## A parameter change sends the same input again: its file is kept. A change
## upstream writes it again.
func input_tests() -> unit!:
    let w = Workspace()
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Grid", 0.0, 0.0)
    let grid = w.network.selected
    node_commands.set_parameter(w, 1, 400.0)
    node_commands.add_node(w, "Script", 0.0, 100.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    let scripts = (w.network.sources else trap("sources")).scripts
    assert(w.network.require(id).failure == "", w.network.require(id).failure)
    let first = scripts.inputs_kept
    let lift = code_cook.spares_of(w.network.require(id).texts[code_cook.spares_slot])[0]
    for row in [1.0, 2.0, 3.0]:
        w.network.selected = id
        node_commands.set_parameter(w, lift.slot, row)
    assert(math.abs(top(w) - 3.0) < 1e-5)
    assert(scripts.inputs_kept == first + 3, f"{scripts.inputs_kept}")
    assert(w.network.require(id).console.contains("input kept"), w.network.require(id).console)
    w.network.selected = grid
    node_commands.set_parameter(w, 0, 3.0)
    assert(scripts.inputs_kept == first + 3)
    scripts.close()
    print(f"PASS script keeps an unchanged input's file: {w.network.require(id).console.split("\n")[0]}")

## The check at a pause, in process: an error at its line, then a clean text.
func check_tests() -> unit!:
    let checker = ScriptChecker()
    let broken = "from luce_geocore.script import Cook\n\npub func cook(k: Cook*) -> !:\n    let x: i64 = \"text\"\n"
    checker.start(broken, "", "", "")
    finish(checker)
    assert(checker.failure.contains("line 4, column"), checker.failure)
    let failed_in = checker.seconds
    let warmed_in = checker.warmed_in
    checker.start(broken.replace("let x: i64 = \"text\"", "k.warning(\"fine\")"), "", "", "")
    finish(checker)
    assert(checker.failure == "", checker.failure)
    let clean_in = checker.seconds
    # A keystroke's check, once the packages are kept.
    var total = 0.0
    for i in 0..<20:
        checker.start(broken.replace("let x: i64 = \"text\"", f"k.warning(\"fine {i}\")"), "", "", "")
        finish(checker)
        total += checker.seconds
    print(f"PASS script check while typing: the set warmed off the UI thread in {format_ms(warmed_in)}, an error at its line in {format_ms(failed_in)}, clean in {format_ms(clean_in)}, then {format_ms(total / 20.0)} a check")

## The check with every package from the registry (a released editor, or a
## clone from the registry): the set is locked and synced first, and an error
## is still found at its line.
func registry_check_tests() -> unit!:
    let saved = process.variable("LUCED_GEOCORE") else ""
    process.unset_variable("LUCED_GEOCORE")
    let checker = ScriptChecker()
    let broken = "from luce_geocore.script import Cook\n\npub func cook(k: Cook*) -> !:\n    let x: i64 = \"text\"\n"
    checker.start(broken, "", "", "")
    finish(checker)
    let manifest = files.read_text(paths.join(paths.directory(paths.directory(checker.file)), "package.prisma"))
    if saved != "":
        process.set_variable("LUCED_GEOCORE", saved)
    assert(not manifest.contains("str path ="), manifest)
    assert(checker.failure.starts_with("line 4, column"), checker.failure)
    checker.start(broken.replace("let x: i64 = \"text\"", "k.warning(\"fine\")"), "", "", "")
    finish(checker)
    assert(checker.failure == "", checker.failure)
    checker.close()
    print(f"PASS script check with registry packages: locked, synced and warmed in {format_ms(checker.warmed_in)}, an error at its line")

## A check that cannot check fails, never passes: a package that cannot be
## found (its sync fails), and an import that resolves to nothing.
func failing_check_tests() -> unit!:
    let checker = ScriptChecker()
    let clean = "from luce_geocore.script import Cook\n\npub func cook(k: Cook*) -> !:\n    k.warning(\"fine\")\n"
    checker.start(clean, "dymokomi/luce-no-such-package", "", "")
    finish(checker)
    assert(checker.failure.starts_with("the script's packages could not be synced"), checker.failure)
    let unsynced = checker.failure
    # Every check of that set says so, not only the first.
    checker.start(clean.replace("fine", "still"), "dymokomi/luce-no-such-package", "", "")
    finish(checker)
    assert(checker.failure == unsynced, checker.failure)
    # An import of a package the set does not have.
    checker.start("from luce_no_such_package.thing import Thing\n" + clean, "", "", "")
    finish(checker)
    assert(checker.failure != "", "an unresolved import passed the check")
    let unresolved = checker.failure
    checker.start(clean, "", "", "")
    finish(checker)
    assert(checker.failure == "", checker.failure)
    checker.close()
    print(f"PASS script check fails when it cannot check: [{unsynced}] [{unresolved}]")

## The editor's set from a clone of luced-3d whose packages luc synced from
## the registry (no checkouts beside it): each is found under .luc/deps by its
## module name and named by its registry version, so a script shares it.
func editor_set_tests() -> unit!:
    let saved = process.variable("LUCED_GEOCORE") else ""
    process.unset_variable("LUCED_GEOCORE")
    let here = process.current_directory()
    with files.TemporaryDirectory() as directory:
        let clone = paths.join(directory.path(), "luced-3d")
        files.ensure_directory(clone)
        files.write_text(paths.join(clone, "package.prisma"), "#prisma 4.0\ndef package \"luced-3d\" {\n    str owner = \"dymokomi\"\n    str version = \"0.1.0\"\n    def dependency \"luce-geocore\" {\n        str owner = \"dymokomi\"\n        str path = \"../luce-geocore\"\n    }\n}\n")
        for row in [["luce_geocore", "0.7.4", "luce-std"], ["luce_std", "0.7.2", ""]]:
            let name = row[0]
            let version = row[1]
            let needs = row[2]
            let folder = paths.join(paths.join(paths.join(clone, ".luc"), "deps"), name)
            files.ensure_directory(folder)
            let dependency = f"    def dependency \"{needs}\" {{\n        str owner = \"dymokomi\"\n        str path = \"../{needs}\"\n    }}\n" if needs != "" else ""
            files.write_text(paths.join(folder, "package.prisma"), f"#prisma 4.0\ndef package \"{name.replace("_", "-")}\" {{\n    str owner = \"dymokomi\"\n    str version = \"{version}\"\n{dependency}}}\n")
        process.change_directory(clone)
        let editor = editor_packages()
        process.change_directory(here)
        if saved != "":
            process.set_variable("LUCED_GEOCORE", saved)
        assert("luce-geocore" in editor and "luce-std" in editor, "the clone's .luc/deps packages were not found")
        let needed = dependencies("", "", editor)
        assert(needed.length == 2)
        for d in needed:
            assert(d.path == "", f"{d.name} by its folder {d.path}")
        assert(needed[0].name == "luce-geocore" and needed[0].version == "0.7.4", needed[0].block())
        assert(needed[1].name == "luce-std" and needed[1].version == "0.7.2", needed[1].block())
    print("PASS script editor set of a registry clone: its .luc/deps packages, by registry version")

## Completion and hover on the node's check package, with luce-image as a
## package: `k.` and `g.` list the API's members, a package's function shows
## its signature and doc.
func help_tests() -> unit!:
    let checker = ScriptChecker()
    let script = "from luce_geocore.script import Cook\nfrom luce_image.image import Image\n\npub func cook(k: Cook*) -> !:\n    let g = k.geometry()\n    let opened = try Image.open(k.file(\"image\", \"a.jpg\"))\n    let image = try opened.get()\n    print(f\"{try image.width()} {g.point_count()}\")\n"
    # Preparing a new set returns at once: its sync and first check run off
    # this thread, and it is not ready (completion waits) until they end.
    let preparing = time.now()
    let file = checker.prepare("luce-image", "", "", script)
    let prepared_in = float(time.now() - preparing) / 1000000000.0
    assert(not checker.ready(), "a new set is warming")
    checker.start(script, "luce-image", "", "")
    finish(checker)
    assert(checker.ready(), "the set is warm")
    assert(checker.failure == "", checker.failure)
    let k = labels(file, script + "    k.", 9, 7)
    for name in ["geometry", "input", "f32", "set_geometry", "warning"]:
        assert(name in k, f"k. lacks {name}: {k}")
    let g = labels(file, script + "    g.", 9, 7)
    for name in ["positions", "add_point", "add_volume", "point_count"]:
        assert(name in g, f"g. lacks {name}: {g}")
    let image = labels(file, script + "    image.", 9, 11)
    for name in ["width", "height", "get_sample"]:
        assert(name in image, f"image. lacks {name}: {image}")
    let started = time.now()
    let hovered = ScriptHelp.hover(file, script, 6, 29)
    let hover_in = float(time.now() - started) / 1000000000.0
    assert(hovered.starts_with("open(path: str"), hovered)
    assert(hovered.contains("\nRead the file now"), hovered)
    # Underlined at its line.
    let broken = script.replace("g.point_count()", "g.point_total()")
    checker.start(broken, "luce-image", "", "")
    finish(checker)
    assert(checker.failure.starts_with("line 8, column"), checker.failure)
    print(f"PASS script completion and hover: k., g., a luce-image value, Image.open's doc (prepared in {format_ms(prepared_in)}, hover {format_ms(hover_in)})")

## The labels completion gives at `line`, `column` of `text`.
func labels(file: str, text: str, line: int, column: int) -> list[str]!:
    let found = ScriptHelp.complete(file, text, line, column)
    let result: list[str] = []
    for at in 0..<found.count():
        result.append(found.label(at))
    return result

func format_ms(seconds: float) -> str:
    return f"{math.round(seconds * 100000.0) / 100.0} ms"

func finish(checker: ScriptChecker) -> unit!:
    let deadline = time.now() + 60000000000
    while not checker.poll():
        assert(time.now() < deadline, "the check did not finish")
        thread.sleep(5)

## Past the limit, the least recently used entries go; what is kept stays.
func cache_tests() -> unit!:
    let saved = process.variable("LUC_HOME") else ""
    let home = paths.join(files.home_directory(), f"luced-script-cache-{time.now()}")
    process.set_variable("LUC_HOME", home)
    let build = paths.join(paths.join(home, "scripts"), "build")
    let made: list[str] = []
    for i in 0..<4:
        let folder = paths.join(build, f"entry{i}")
        files.ensure_directory(folder)
        files.write_text(paths.join(folder, "script"), "x".repeat(1000))
        files.write_text(paths.join(folder, ".used"), f"{1000 + i}")
        made.append(folder)
    # The oldest (entry0) is kept by a node; entry1 and entry2 go to fit 2 KB.
    let removed = script_cache.evict([made[0]], 2100)
    assert(removed >= 2000, f"{removed}")
    _ = files.metadata(paths.join(made[0], "script"))
    var gone = 0
    for i in 1..<3:
        _ = files.metadata(paths.join(made[i], "script")) catch failure:
            gone += 1
            continue
    assert(gone == 2, f"{gone}")
    _ = files.metadata(paths.join(made[3], "script"))
    # The settings file is written with the default, and read back.
    assert(script_cache.limit_bytes() == script_cache.default_megabytes * 1048576)
    assert(files.read_text(script_cache.settings_path()).contains("cache_megabytes"))
    files.write_text(script_cache.settings_path(), "#prisma 4.0\ndef settings \"scripts\" {\n    int cache_megabytes = 3\n}\n")
    assert(script_cache.limit_bytes() == 3 * 1048576)
    files.remove_tree(home)
    if saved == "":
        process.unset_variable("LUC_HOME")
    else:
        process.set_variable("LUC_HOME", saved)
    print("PASS script cache: least recently used entries past the limit go; the setting is read")

## A registry-named package takes the workspace's checkout, so it shares the
## editor's luce-std; and such a script builds and cooks (from the registry,
## with no checkouts).
func package_tests(checkouts: bool) -> unit!:
    if checkouts:
        let editor = editor_packages()
        let std = editor["luce-std"] else trap("the editor set has no luce-std")
        var image = ""
        for d in dependencies("dymokomi/luce-image@^0.45.0", "", editor):
            if d.name == "luce-image":
                image = d.path
        assert(image != "", "luce-image came from the registry")
        let manifest = files.read_text(paths.join(image, "package.prisma"))
        assert(manifest.contains("luce-std"))
        assert(files.canonical(paths.join(paths.directory(image), "luce-std")) == std)
    let w = Workspace()
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Script", 0.0, 0.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    script_node.set_packages(w, id, "dymokomi/luce-image@^0.45.0")
    script_node.set_code(w, id, "from luce_geocore.script import Cook\nfrom luce_image.image import Image\n\npub func cook(k: Cook*) -> !:\n    let opened = Image.open(\"missing.png\") catch failure:\n        k.warning(\"no image, as expected\")\n        return\n    opened.release()\n")
    assert(w.network.require(id).failure == "", w.network.require(id).failure)
    assert(w.network.require(id).warning.contains("no image"), w.network.require(id).warning)
    (w.network.sources else trap("sources")).scripts.close()
    print(f"PASS script packages resolve as the editor's build did (one luce-std{", checkouts" if checkouts else ", registry"})")