Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.2

0.11.2 / tests / editor / file_options_tests.luc

## Format-scoped import settings survive projects, history and worker caches.
from luce_std import files
import node_catalog
import project_file
from app import Luce3D
from network import Network
from workspace import Workspace
from luce_prism.prism import Document
from project_graph import write_graph, read_graph
from compute_request import RequestWriter, read_request
from stamps import stamp_graph
import time
import node_commands

pub func file_options_tests() -> unit!:
    for path in ["shape.step", "shape.STP", " shape.STEP "]:
        let fields = node_catalog.parameters("File", path)
        assert(fields.length == 1 and fields[0].slot == 0)
        assert(fields[0].decimals == 9 and fields[0].initial == 0.0)
    for path in ["", "shape.obj"]:
        assert(node_catalog.parameters("File", path).length == 0)
    # The tolerance rules themselves are luce-step's tests; this file checks the node.
    let text = files.read_text("tests/fixtures/cylinder.step").replace("(2.,0.,0.)", "(2.00005,0.,0.)")
    files.write_text("tolerant-import.step", text)
    let editor = Luce3D()
    let w = editor.workspace
    node_commands.add_node(w, "File", 0.0, 0.0)
    node_commands.set_text(w, 0, "tolerant-import.step")
    let node = w.selected() else trap("File")
    assert(node.failure != "")
    node_commands.set_parameter(w, 0, 0.0001)
    assert(node.failure == "" and node.cache != none)
    # The tolerance is part of the File node's stamp and of its worker body.
    let writer = RequestWriter()
    stamp_graph(w.network)
    let tolerant = node.stamp
    let worker = read_request(writer.encode(w.network, 3), Network(false))
    assert(worker.require(node.id).values[0] == 0.0001 and worker.require(node.id).stamp == tolerant)
    node_commands.set_parameter(w, 0, 0.0002)
    stamp_graph(w.network)
    assert(node.stamp != tolerant)
    let changed = read_request(writer.encode(w.network, 3), worker)
    assert(changed.require(node.id).values[0] == 0.0002)
    w.undo()
    assert((w.selected() else trap("File")).values[0] == 0.0001)
    w.redo()
    assert((w.selected() else trap("File")).values[0] == 0.0002)
    let doc = Document()
    write_graph(doc, w.network)
    let restored = read_graph(doc)
    assert(restored.require(node.id).values[0] == 0.0002)
    assert(project_file.same_graph(w.network, restored))
    # Reusing a File node for a different format hides the STEP-only setting.
    node_commands.set_text(w, 0, "tests/fixtures/cylinder.obj")
    editor.panels.inspector.refresh()
    assert(node_catalog.parameters("File", (w.selected() else trap("File")).texts[0]).length == 0)
    node_commands.set_text(w, 0, "tolerant-import.step")
    editor.panels.inspector.refresh()
    assert(editor.panels.inspector.shown_value(0) == 0.0002)
    # Exercise the actual off-main worker, not just its serialized recipe.
    w.enable_compute()
    settle(w)
    assert(w.notice == "" and w.batches.patch_batch != none)
    node_commands.set_parameter(w, 0, 0.0)
    settle(w)
    assert((w.selected() else trap("File")).failure != "")
    node_commands.set_parameter(w, 0, 0.0001)
    settle(w)
    assert(w.notice == "" and (w.selected() else trap("File")).failure == "")
    assert(w.batches.patch_batch != none)
    editor.close()
    print("PASS STEP import tolerance, format-scoped parameters, exact overrides, projects, undo and worker invalidation")

func settle(workspace: Workspace) -> unit!:
    let session = workspace.compute else trap("compute")
    let deadline = time.now() + 20000000000
    while session.busy and time.now() < deadline:
        _ = workspace.poll_compute()
    assert(not session.busy)