dymokomi/luced
Source of release 0.4.1
0.4.1 / tests / configuration.luc
from configuration.settings import Settings
from configuration.theme import Appearance
from configuration.store import Store
from luce_ai.config import AiConfig
from editor.application import Editor
from editor.options import Options
from luce_ui.ui import Event, default_font_size
from luce_window.input import EventKind, Key
import configuration.settings as settings
import configuration.theme as theme
from luce_std import files
import workspace.paths as paths
import workspace.operations as operations
pub func configuration(root: str, luce: str, base: str) -> unit!:
let directory = paths.join(root, "configuration")
let store = Store(directory)
let original = store.read_settings()
assert(Settings(original).font_size == default_font_size)
assert(Appearance(store.read_theme()).theme.gutter != Appearance().theme.background)
# The default ai.toml exists and leaves every mode unset.
assert(not AiConfig(store.read_ai()).ready("ask"))
files.write_text(store.ai_path, "[ask]\nmodel = \"deepseek/deepseek-chat\"\nsystem = \"Be terse.\"\n[openrouter]\napi_key = \"sk-test\"\n")
files.write_text(store.settings_path, "[editor]\nfont_size = 17.0 # preserved\nfont_family = 'Monospace # λ'\n")
let reopened = Store(directory)
assert(reopened.read_settings().contains("# preserved"))
assert(Settings(reopened.read_settings()).font_family == "Monospace # λ")
assert(Settings("[editor]\nfont_size = 1_4.5e0\n").font_size == 14.5)
for text in ["[editor]\nfont_size = 0\n", "[editor]\nfont_size = 14\nfont_size = 15\n", "[editor]\nfont_size = 014\n", "[editor]\nfont_size = 1__4\n", "[editor]\nfont_size = 14.\n", "[unknown]\n", "[editor]\nfont_szie = 14\n", "[editor]\nfont_family = \"unterminated\n"]:
var refused = false
_ = Settings(text) catch failure:
refused = true
recover Settings()
assert(refused)
var refused = false
_ = Appearance("[colors]\ngutter = \"#wrong!\"\n") catch failure:
refused = true
recover Appearance()
assert(refused)
files.write_text(store.settings_path, settings.defaults())
assert(Appearance("[popup]\nshadow_opacity = 0.25\nshadow_offset = 6\n").theme.shadow_opacity == 0.25)
assert(Appearance("[layout]\nheader_inset_cells = 2\n").theme.header_inset_cells == 2.0)
refused = false
_ = Appearance("[popup]\nshadow_opacity = 2\n") catch failure:
refused = true
recover Appearance()
assert(refused)
let fixture = paths.join(root, "configuration-source.luc")
files.write_text(fixture, "pub func sample():\n let value = 1\n")
let editor = Editor(Options([fixture, "--config-dir", directory, "--luce", luce, "--base", base]))
editor.app.layout(1000.0, 700.0)
let document_panel = editor.workspace.current_panel() else error(files.failed, "document panel was not opened")
assert(document_panel.layout().contains_focus())
# The AI config loads at startup; a filled ai.toml makes its mode ready.
assert(editor.configuration.ai().ready("ask") and editor.configuration.ai().openrouter.api_key == "sk-test")
assert(editor.configuration.ai().ask.system == "Be terse.")
editor.configuration.edit_ai()
let ai_doc = editor.workspace.current_document() else error(files.failed, "ai.toml did not open")
assert(ai_doc.path == store.ai_path)
# A malformed ai.toml is rejected on reload; the last good config is kept.
files.write_text(store.ai_path, "[ask]\nprovider = \"mistral\"\nmodel = \"large\"\n")
editor.configuration.reload()
assert(editor.configuration.ai().ready("ask") and store.read_ai().contains("mistral"))
files.write_text(store.ai_path, "[spellcheck]\nprovider = \"claude_code\"\nmodel = \"sonnet\"\n[claude_code]\ncommand = \"claude\"\n")
editor.configuration.reload()
assert(editor.configuration.ai().ready("spellcheck") and not editor.configuration.ai().ready("ask"))
editor.workspace.open(fixture)
editor.app.layout(1000.0, 700.0)
editor.actions.files.new_file.trigger()
editor.app.layout(1000.0, 700.0)
assert(editor.view.prompt.is_open())
for scalar in [110, 101, 119, 45, 955, 46, 108, 117, 99]:
editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = scalar))
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(not editor.view.prompt.is_open() and files.read_text(paths.join(root, "new-λ.luc")) == "")
editor.workspace.delete_entry(paths.join(root, "new-λ.luc"))
editor.workspace.open(fixture)
editor.app.layout(1000.0, 700.0)
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.f6))
editor.app.layout(1000.0, 700.0)
assert(editor.view.output.body.layout().contains_focus() and not document_panel.layout().contains_focus())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.f6, shift = true))
editor.app.layout(1000.0, 700.0)
assert(document_panel.layout().contains_focus())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.left, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(editor.view.sidebar.body.layout().contains_focus() and not document_panel.layout().contains_focus())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.right, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(document_panel.layout().contains_focus())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.down, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(editor.view.output.body.layout().contains_focus() and not document_panel.layout().contains_focus())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.up, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(document_panel.layout().contains_focus())
let wrap_editor = editor.workspace.current_editor() else error(files.failed, "no editor to wrap")
assert(not wrap_editor.wrapping())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(wrap_editor.wrapping())
editor.workspace.new_tab()
editor.app.layout(1000.0, 700.0)
let wrapped_tab = editor.workspace.current_editor() else error(files.failed, "no fresh tab")
assert(wrapped_tab.wrapping())
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.z, control = true, alt = true))
editor.app.layout(1000.0, 700.0)
assert(not wrap_editor.wrapping() and not wrapped_tab.wrapping())
editor.workspace.open(fixture)
editor.app.layout(1000.0, 700.0)
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.p, control = true))
editor.app.layout(1000.0, 700.0)
for scalar in [101, 100, 105, 116, 32, 115, 101, 116, 116, 105, 110, 103, 115]:
editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = scalar))
# Both Edit Settings and Edit AI Settings match; the first is Edit Settings.
assert(editor.view.palette.result_count() == 2)
editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.enter))
assert(not editor.view.palette.is_open())
let active = editor.workspace.current_editor() else error(files.failed, "settings were not opened")
assert(active.text() == settings.defaults())
files.write_text(store.theme_path, "[colors]\ngutter = \"#445566\"\nactive_border = \"#668899\"\n")
editor.configuration.reload()
editor.app.layout(1000.0, 700.0)
let changed = editor.view.body.layout().theme().gutter
assert(changed == Appearance(store.read_theme()).theme.gutter)
files.write_text(store.theme_path, "[colors]\ngutter = \"invalid\"\n")
editor.configuration.reload()
assert(editor.view.body.layout().theme().gutter == changed and store.read_theme().contains("invalid"))
files.write_text(store.theme_path, theme.defaults())
editor.configuration.reload()
editor.workspace.open(fixture)
editor.workspace.rename_entry(fixture, "renamed-λ.luc")
let renamed = paths.join(root, "renamed-λ.luc")
let source = editor.workspace.current_editor() else error(files.failed, "renamed source was lost")
source.insert("# retained\n")
editor.app.layout(1000.0, 700.0)
assert(source.has_changes())
editor.workspace.save()
source.set_selection(0, 0)
editor.app.layout(1000.0, 700.0)
assert(files.read_text(renamed).starts_with("# retained") and not source.has_changes())
let copied = operations.paste(renamed, paths.parent(renamed), move = false)
let second = operations.paste(renamed, paths.parent(renamed), move = false)
assert(copied != second and files.read_text(copied) == files.read_text(renamed))
refused = false
_ = operations.create_file(root, paths.name(renamed)) catch failure:
refused = true
recover ""
assert(refused and files.read_text(renamed).starts_with("# retained"))
source.insert("unsaved")
refused = false
editor.workspace.delete_entry(renamed) catch failure: refused = true
assert(refused and files.metadata(renamed).kind == files.FileKind.regular)
source.undo()
editor.workspace.delete_entry(renamed)
editor.workspace.create_folder(root, "nested-λ")
let nested = paths.join(root, "nested-λ")
editor.workspace.create_file(nested, "child.luc")
editor.workspace.rename_entry(nested, "renamed-folder")
let nested_source = editor.workspace.current_editor() else error(files.failed, "nested source was lost")
nested_source.insert("# preserved after folder rename\n")
editor.workspace.save()
assert(files.read_text(paths.join(root, "renamed-folder/child.luc")).contains("preserved"))
editor.workspace.delete_entry(paths.join(root, "renamed-folder"))
files.remove_file(copied)
files.remove_file(second)
editor.close()