mod browser;
use crate::folder::ServedFolder;
use anyhow::{Result, anyhow, bail};
use browser::Browser;
pub use browser::imported_module;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
sync::{Arc, Mutex, OnceLock},
};
pub trait Package: Send + Sync {
fn invoke(&self, request: Value) -> Result<Value>;
fn process(&self) -> Option<Process> {
None
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Process {
pub command: Vec<String>,
pub folder: PathBuf,
}
#[derive(Clone, Debug)]
pub struct Live {
pub key: String,
pub id: String,
pub process: Option<Process>,
pub config: String,
pub folder: PathBuf,
pub rest: String,
}
pub trait Loader: Send + Sync {
fn load(&self, path: &Path) -> Result<Arc<dyn Package>>;
}
static LOADER: OnceLock<Arc<dyn Loader>> = OnceLock::new();
pub fn install_loader(loader: Arc<dyn Loader>) {
let _ = LOADER.set(loader);
}
static INSTANCES: OnceLock<Mutex<BTreeMap<PathBuf, Vec<Binding>>>> = OnceLock::new();
fn instances() -> &'static Mutex<BTreeMap<PathBuf, Vec<Binding>>> {
INSTANCES.get_or_init(Default::default)
}
pub const LIVE: &str = "live";
#[derive(Clone, Default)]
struct Local {
session: Option<Arc<dyn Package>>,
packages: BTreeMap<String, Config>,
entry: Option<PathBuf>,
}
static LOCAL: OnceLock<Mutex<BTreeMap<ServedFolder, Local>>> = OnceLock::new();
fn local() -> &'static Mutex<BTreeMap<ServedFolder, Local>> {
LOCAL.get_or_init(Default::default)
}
pub fn serve_packages(scope: &ServedFolder, home: &Path, mut packages: BTreeMap<String, Config>) {
for config in packages.values_mut() {
if let Some(path) = &mut config.package {
*path = home.join(&*path).to_string_lossy().into_owned();
}
}
local()
.lock()
.unwrap()
.entry(scope.clone())
.or_default()
.packages = packages;
}
pub fn serve_live(scope: &ServedFolder, package: Arc<dyn Package>) {
local()
.lock()
.unwrap()
.entry(scope.clone())
.or_default()
.session = Some(package);
}
pub fn serve_entry(scope: &ServedFolder, document: PathBuf) {
local()
.lock()
.unwrap()
.entry(scope.clone())
.or_default()
.entry = Some(document);
}
pub(crate) fn configured(
scope: &ServedFolder,
document: BTreeMap<String, Config>,
) -> BTreeMap<String, Config> {
let mut configs = local()
.lock()
.unwrap()
.get(scope)
.map(|local| local.packages.clone())
.unwrap_or_default();
configs.extend(document);
configs
}
fn enabled(config: &serde_yaml::Value) -> Result<bool> {
match config {
serde_yaml::Value::Null => Ok(true),
serde_yaml::Value::Mapping(map) => match map.get("enabled") {
None => Ok(true),
Some(serde_yaml::Value::Bool(enabled)) => Ok(*enabled),
Some(_) => bail!("extensions: {LIVE}: config: enabled is not true or false"),
},
_ => bail!("extensions: {LIVE}: config is not a mapping"),
}
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub enabled: Option<bool>,
pub package: Option<String>,
#[serde(default)]
pub config: serde_yaml::Value,
}
#[derive(Clone)]
pub struct Binding(Arc<Mutex<State>>);
impl std::fmt::Debug for Binding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("ExtensionBinding")
}
}
#[derive(Clone)]
struct State {
id: String,
configuration: String,
owner: String,
system: bool,
package: Arc<dyn Package>,
scope: ServedFolder,
config: Value,
folder: PathBuf,
input: Option<PathBuf>,
descriptor: Value,
places: crate::document::Places,
browser: Arc<Browser>,
revision: String,
error: Option<String>,
sources: BTreeMap<String, String>,
}
impl Binding {
fn base(s: &State) -> String {
format!(
"{}/__devpage/extensions/{}/{}/",
s.scope.mount(),
s.owner,
s.id
)
}
pub fn address(&self) -> (String, String) {
let s = self.0.lock().unwrap();
(s.id.clone(), Self::base(&s))
}
pub fn is_session(&self) -> bool {
self.0.lock().unwrap().system
}
pub fn places(&self) -> crate::document::Places {
self.0.lock().unwrap().places.clone()
}
pub fn widget_urls(&self) -> BTreeMap<String, String> {
let s = self.0.lock().unwrap();
s.browser.widgets(&Self::base(&s))
}
pub fn style_urls(&self) -> Vec<String> {
let s = self.0.lock().unwrap();
s.browser.styles(&Self::base(&s))
}
pub fn view(&self, file: &Path) -> Result<Value> {
let s = self.0.lock().unwrap();
let path = s
.scope
.name(file)
.ok_or_else(|| anyhow!("View outside served folder"))?;
drop(s);
self.call("view", json!({"path":path}))
}
pub fn snapshot(&self) -> Self {
Self(Arc::new(Mutex::new(self.0.lock().unwrap().clone())))
}
fn call(&self, op: &str, extra: Value) -> Result<Value> {
let s = self.0.lock().unwrap();
if let Some(error) = &s.error {
bail!("Extension preparation failed: {error}");
}
let mut request = extra;
request["op"] = op.into();
request["instance"] = s.owner.clone().into();
request["base"] = format!(
"{}/__devpage/extensions/{}/{}/",
s.scope.mount(),
s.owner,
s.id
)
.into();
request["revision"] = s.revision.clone().into();
s.package.invoke(request)
}
pub fn input(&self) -> Option<PathBuf> {
self.0.lock().unwrap().input.clone()
}
pub fn owns(&self, file: &Path) -> bool {
let s = self.0.lock().unwrap();
if file.is_absolute()
&& !s
.input
.as_ref()
.is_some_and(|input| file.starts_with(input))
{
return false;
}
let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
s.descriptor["file_extensions"]
.as_array()
.is_some_and(|a| a.iter().any(|v| v.as_str() == Some(ext)))
}
pub fn prepare(&self) -> Result<bool> {
let mut s = self.0.lock().unwrap();
let mut sources = BTreeMap::new();
fn read(
scope: &ServedFolder,
p: &Path,
files: &mut BTreeMap<String, String>,
extensions: &[Value],
) -> Result<()> {
if std::fs::symlink_metadata(p)?.file_type().is_symlink() {
bail!("Symlinked extension input {}", p.display());
}
let canonical = p.canonicalize()?;
if !canonical.starts_with(scope.path()) {
bail!("Extension input outside served folder");
}
if p.is_dir() {
for entry in std::fs::read_dir(p)? {
let entry = entry?;
if !entry.file_name().to_string_lossy().starts_with('.') {
read(scope, &entry.path(), files, extensions)?;
}
}
} else if p
.extension()
.and_then(|e| e.to_str())
.is_some_and(|ext| extensions.iter().any(|v| v.as_str() == Some(ext)))
{
files.insert(
scope
.name(p)
.ok_or_else(|| anyhow!("Input outside served folder"))?,
std::fs::read_to_string(p)?,
);
}
Ok(())
}
if let Some(input) = &s.input {
let extensions = s.descriptor["input_extensions"]
.as_array()
.ok_or_else(|| anyhow!("Extension must declare input_extensions"))?;
if let Err(error) = read(&s.scope, input, &mut sources, extensions) {
s.error = Some(error.to_string());
return Err(error);
}
}
if s.error.is_none() && s.sources == sources && !s.revision.is_empty() {
return Ok(false);
}
let reply = match s
.package
.invoke(json!({"op":"prepare","instance":s.owner,"sources":sources}))
{
Ok(reply) => reply,
Err(error) => {
s.error = Some(format!("{error:#}"));
return Err(error);
}
};
s.revision = reply["revision"]
.as_str()
.ok_or_else(|| anyhow!("Extension preparation omitted revision"))?
.into();
s.sources = sources;
s.error = None;
Ok(true)
}
pub fn grammar(&self) -> Option<String> {
self.0.lock().unwrap().descriptor["grammar"]
.as_str()
.map(String::from)
}
pub fn transform(&self, file: &Path, source: &str) -> Result<String> {
let s = self.0.lock().unwrap();
let path = s
.scope
.name(file)
.ok_or_else(|| anyhow!("Page outside served folder"))?;
drop(s);
let reply = self.call("augment", json!({"path":path,"source":source}))?;
Ok(reply["source"].as_str().unwrap_or(source).into())
}
pub fn embed(&self, file: &Path, target: &str, label: &str, identity: usize) -> Result<String> {
let s = self.0.lock().unwrap();
let path = s
.scope
.name(file)
.ok_or_else(|| anyhow!("Page outside served folder"))?;
drop(s);
Ok(self.call(
"embed",
json!({"page":path,"target":target,"label":label,"identity":identity}),
)?["html"]
.as_str()
.ok_or_else(|| anyhow!("Missing embed HTML"))?
.into())
}
pub fn ids(&self, file: &Path) -> Result<Vec<String>> {
let s = self.0.lock().unwrap();
let path = s
.scope
.name(file)
.ok_or_else(|| anyhow!("Target outside served folder"))?;
drop(s);
Ok(self.call("targets", json!({"path":path}))?["ids"]
.as_array()
.into_iter()
.flatten()
.filter_map(|v| v.as_str().map(String::from))
.collect())
}
}
pub fn configure(
doc: &Path,
base: &Path,
scope: &ServedFolder,
mut configs: BTreeMap<String, Config>,
) -> Result<()> {
let live_enabled = configs
.get(LIVE)
.is_none_or(|config| config.enabled != Some(false));
configs.retain(|_, config| config.enabled != Some(false));
let own = match configs.get(LIVE) {
Some(config) if config.package.is_none() => configs.remove(LIVE).map(|c| c.config),
_ => None,
};
let on = own.as_ref().map(enabled).transpose()?.unwrap_or(true);
let session = (on && live_enabled && !configs.contains_key(LIVE))
.then(|| {
local()
.lock()
.unwrap()
.get(scope)
.and_then(|local| local.session.clone())
})
.flatten();
let mut guard = instances().lock().unwrap();
if configs.is_empty() && session.is_none() {
guard.remove(doc);
return Ok(());
}
let configuration = serde_json::to_string(&(&configs, &own, session.is_some()))?;
if guard.get(doc).is_some_and(|bindings| {
bindings.iter().all(|b| {
let s = b.0.lock().unwrap();
s.configuration == configuration && s.scope == *scope
})
}) {
return Ok(());
}
let mut bindings = vec![];
let mut parts = BTreeMap::new();
let mut claims: std::collections::BTreeSet<String> = ["md", "yml", "yaml", "design"]
.into_iter()
.map(String::from)
.collect();
for (id, config) in configs {
let Some(path) = &config.package else {
bail!("extensions: {id}: package is required — {id} is not a system extension");
};
let loader = LOADER
.get()
.ok_or_else(|| anyhow!("Native extension loader unavailable"))?;
let package = loader
.load(&base.join(path))
.map_err(|e| anyhow!("Extension {id}: {e:#}"))?;
let owner = format!(
"{:x}",
Sha256::digest(format!("{}:{id}:{configuration}", doc.display()))
)[..16]
.to_string();
let binding = bind(
Bound {
id: id.clone(),
owner,
system: false,
package,
config: serde_json::to_value(&config.config)?,
folder: base,
},
&configuration,
scope,
&mut claims,
)?;
if let Some(path) = binding.0.lock().unwrap().descriptor["world"].as_str() {
parts.insert(id, path.to_owned());
}
bindings.push(binding);
}
if let Some(package) = session {
let mut config = serde_json::to_value(own.unwrap_or_default())?;
if !parts.is_empty() {
if config["world"].is_null() {
config["world"] = json!({});
}
let world = config["world"]
.as_object_mut()
.ok_or_else(|| anyhow!("live: world must be a mapping"))?;
for (part, path) in parts {
world.insert(part, path.into());
}
}
let owner = format!(
"{:x}",
Sha256::digest(format!("{}:{LIVE}", scope.path().display()))
)[..16]
.to_string();
bindings.push(bind(
Bound {
id: LIVE.to_string(),
owner,
system: true,
package,
config,
folder: scope.path(),
},
&configuration,
scope,
&mut claims,
)?);
}
guard.insert(doc.to_path_buf(), bindings);
Ok(())
}
struct Bound<'a> {
id: String,
owner: String,
system: bool,
package: Arc<dyn Package>,
config: Value,
folder: &'a Path,
}
fn bind(
bound: Bound,
configuration: &str,
scope: &ServedFolder,
claims: &mut std::collections::BTreeSet<String>,
) -> Result<Binding> {
let Bound {
id,
owner,
system,
package,
config,
folder,
} = bound;
let descriptor =
package.invoke(json!({"op":"create","instance":owner,"id":id,"config":config}))?;
let none = Vec::new();
let listed = |field: &str| -> Result<&Vec<Value>> {
match &descriptor[field] {
Value::Null => Ok(&none),
Value::Array(listed) => Ok(listed),
_ => bail!("Extension {id} {field} is not a list"),
}
};
let reads = !listed("input_extensions")?.is_empty();
for suffix in listed("file_extensions")? {
let suffix = suffix
.as_str()
.filter(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric()))
.ok_or_else(|| anyhow!("Extension {id} invalid file suffix"))?;
if !claims.insert(suffix.into()) {
bail!("Extension {id} file format {suffix} is already claimed");
}
}
if let Some(grammar) = descriptor["grammar"].as_str() {
let syntax = syntect::parsing::SyntaxDefinition::load_from_str(grammar, true, None)
.map_err(|e| anyhow!("Extension {id} grammar: {e}"))?;
if syntax
.file_extensions
.iter()
.any(|s| crate::highlight::builtin_language(s))
{
bail!("Extension {id} cannot replace a built-in language");
}
}
let input = match descriptor["input"].as_str() {
Some(input) => {
let input = folder.join(input);
if !input.canonicalize()?.starts_with(scope.path()) {
bail!("Extension {id} input outside served folder");
}
Some(input)
}
None if reads || !listed("file_extensions")?.is_empty() => {
bail!("Extension input missing")
}
None => None,
};
let browser = Browser::new(
&descriptor,
&format!("{}/__devpage/extensions/{owner}/{id}/", scope.mount()),
)?;
let places = serde_json::from_value(descriptor.get("places").cloned().unwrap_or(json!({})))?;
let binding = Binding(Arc::new(Mutex::new(State {
id,
configuration: configuration.to_string(),
owner,
system,
package,
scope: scope.clone(),
config,
folder: folder.to_path_buf(),
input,
descriptor,
places,
browser: Arc::new(browser),
revision: String::new(),
error: None,
sources: BTreeMap::new(),
})));
binding.prepare()?;
Ok(binding)
}
pub fn for_document(doc: &Path) -> Vec<Binding> {
instances()
.lock()
.unwrap()
.get(doc)
.cloned()
.unwrap_or_default()
}
pub fn input_roots(scope: &ServedFolder) -> Vec<PathBuf> {
instances()
.lock()
.unwrap()
.values()
.flatten()
.filter(|b| b.0.lock().unwrap().scope == *scope)
.filter_map(Binding::input)
.collect()
}
pub fn changed(scope: &ServedFolder, files: &[PathBuf]) -> bool {
let all: Vec<_> = instances()
.lock()
.unwrap()
.values()
.flatten()
.filter(|b| b.0.lock().unwrap().scope == *scope)
.cloned()
.collect();
let mut changed = false;
for b in all {
let Some(input) = b.input() else {
continue;
};
if files
.iter()
.any(|p| p.starts_with(&input) || input.starts_with(p))
{
changed |= b.prepare().unwrap_or(true);
}
}
changed
}
pub fn for_file(file: &Path, folder: &crate::folder::Folder) -> Vec<Binding> {
let path = folder
.owner(file)
.and_then(|o| o.document.ok())
.map(|d| d.path())
.unwrap_or_else(|| folder.entry().path());
for_document(&path)
}
pub fn live(path: &str, scope: &ServedFolder) -> Option<Live> {
let (mut live, _) = bound_at(path, scope, None)?;
live.rest = live.rest.strip_prefix("live/")?.to_string();
Some(live)
}
pub fn page(path: &str, scope: &ServedFolder, entry: &Path) -> Option<(Live, String, PathBuf)> {
let (live, document) = bound_at(path, scope, Some(entry))?;
let page = live.rest.strip_prefix("pages/")?.to_string();
(!page.is_empty()).then_some((live, page, document))
}
fn bound_at(path: &str, scope: &ServedFolder, preferred: Option<&Path>) -> Option<(Live, PathBuf)> {
let suffix = path
.trim_start_matches('/')
.strip_prefix("__devpage/extensions/")?;
let (owner, tail) = suffix.split_once('/')?;
let (id, rest) = tail.split_once('/')?;
let guard = instances().lock().unwrap();
let found = |(document, bindings): (&PathBuf, &Vec<Binding>)| {
bindings.iter().find_map(|b| {
let s = b.0.lock().unwrap();
(s.owner == owner && s.id == id && s.scope == *scope).then(|| {
let live = Live {
key: format!("{owner}/{id}"),
id: s.id.clone(),
process: s.package.process(),
config: s.config.to_string(),
folder: s.folder.clone(),
rest: rest.to_string(),
};
(live, document.clone())
})
})
};
let entry = (id == LIVE)
.then(|| {
local()
.lock()
.unwrap()
.get(scope)
.and_then(|local| local.entry.clone())
})
.flatten();
preferred
.or(entry.as_deref())
.and_then(|preferred| guard.get_key_value(preferred))
.and_then(found)
.or_else(|| guard.iter().find_map(found))
}
pub fn bound(key: &str) -> bool {
let guard = instances().lock().unwrap();
guard.values().flatten().any(|b| {
let s = b.0.lock().unwrap();
key.split_once('/') == Some((&s.owner, &s.id))
})
}
pub fn route(path: &str, scope: &ServedFolder) -> Option<Result<Value>> {
let suffix = path
.trim_start_matches('/')
.strip_prefix("__devpage/extensions/")?;
let (owner, tail) = suffix.split_once('/')?;
let (id, tail) = tail.split_once('/')?;
let all: Vec<_> = instances()
.lock()
.unwrap()
.values()
.flatten()
.cloned()
.collect();
all.into_iter()
.find(|b| {
let s = b.0.lock().unwrap();
s.owner == owner && s.id == id && s.scope == *scope
})
.map(|b| {
let response = {
let s = b.0.lock().unwrap();
s.browser.response(tail)
};
response.map_or_else(|| b.call("route", json!({"path":tail})), Ok)
})
}
#[cfg(test)]
mod tests {
use super::*;
use codespec_rust_macro::spec;
struct Toy;
impl Loader for Toy {
fn load(&self, _: &Path) -> Result<Arc<dyn Package>> {
Ok(Arc::new(Toy))
}
}
impl Package for Toy {
fn invoke(&self, r: Value) -> Result<Value> {
Ok(match r["op"].as_str().unwrap() {
"create" => {
json!({"input":r["config"]["input"],"input_extensions":["toy","md"],"file_extensions":["toy"],
"grammar":"%YAML 1.2\n---\nname: Toy\nfile_extensions: [toy]\nscope: source.toy\ncontexts:\n main:\n - match: magic\n scope: keyword.control.toy\n"})
}
"prepare" => {
json!({"revision":format!("{:x}",Sha256::digest(r["sources"].to_string()))})
}
"augment" => {
json!({"source":format!("{}\n\n## Extension content\n",r["source"].as_str().unwrap())})
}
"embed" => {
json!({"html":"<svg role=\"img\"><text>Prepared toy diagram</text></svg>"})
}
"view" => {
json!({"layout":"workspace","title":"Toy","content":format!("<h1>Toy viewer</h1><p>{}</p>",r["revision"].as_str().unwrap())})
}
"targets" => json!({"ids":["main"]}),
"route" => json!({"body":r["revision"],"mime":"text/plain","status":200}),
_ => bail!("Unknown toy operation"),
})
}
}
#[test]
#[spec("hot-reload-works-for-every-page")]
#[spec("a-broken-file-shows-its-error")]
fn another_language_uses_the_same_capture_render_targets_and_watch_contract() {
install_loader(Arc::new(Toy));
let dir =
std::env::temp_dir().join(format!("devpage-extension-contract-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("model/nested")).unwrap();
std::fs::write(dir.join("model/nested/a.toy"), "first").unwrap();
std::fs::write(dir.join("model/ignored.rs"), "not an input").unwrap();
std::fs::write(
dir.join("page.md"),
"# Page\n\n```toy\nmagic\n```\n\n",
)
.unwrap();
std::fs::write(dir.join("devpage.yml"),"kind: site\ntitle: Toy\nroot: page.md\nwatch: [page.md]\nextensions:\n toy:\n package: ./toy-package\n config:\n input: model\n").unwrap();
let dir = dir.canonicalize().unwrap();
let scope = ServedFolder::new(&dir).unwrap();
let doc = crate::document::Document::load(&dir.join("devpage.yml"), &scope).unwrap();
let bindings = for_document(&doc.path());
assert_eq!(bindings.len(), 1);
assert!(!bindings[0].prepare().unwrap());
let first = bindings[0].view(&dir.join("model/nested/a.toy")).unwrap();
let rendered = crate::page::content(
&crate::widgets::Widgets::of(&doc, &crate::widgets::Modules::default()),
&dir.join("page.md"),
std::fs::read_to_string(dir.join("page.md")).unwrap(),
);
assert!(rendered.html().contains("hl-keyword"));
assert!(rendered.html().contains("Prepared toy diagram"));
assert!(rendered.ids().contains("extension-content"));
assert!(
rendered
.embeds()
.iter()
.any(|t| t == "model/nested/a.toy#main")
);
assert_eq!(
bindings[0].ids(&dir.join("model/nested/a.toy")).unwrap(),
vec!["main"]
);
let folder = crate::folder::Folder::new(Arc::new(doc));
assert!(folder.matters(&dir.join("model/nested/new.toy"), false));
std::fs::write(dir.join("model/nested/a.toy"), "second").unwrap();
assert!(changed(&scope, &[dir.join("model/nested/a.toy")]));
assert_ne!(
first,
bindings[0].view(&dir.join("model/nested/a.toy")).unwrap()
);
std::fs::remove_dir_all(dir.join("model")).unwrap();
assert!(bindings[0].prepare().is_err());
assert!(bindings[0].view(&dir.join("model/nested/a.toy")).is_err());
std::fs::create_dir_all(dir.join("model")).unwrap();
assert!(bindings[0].prepare().unwrap());
assert!(bindings[0].view(&dir.join("model/a.toy")).is_ok());
instances().lock().unwrap().remove(&dir.join("devpage.yml"));
std::fs::remove_dir_all(dir).unwrap();
}
struct Session;
impl Package for Session {
fn invoke(&self, r: Value) -> Result<Value> {
Ok(match r["op"].as_str().unwrap() {
"create" => {
json!({"places":{"navbar":["DevpageLiveTasksButton"],"panel":["DevpageLiveTasks"],"float":["DevpageLiveCanvas","DevpageLiveOrb"]}})
}
"prepare" => json!({"revision": "none"}),
_ => json!({}),
})
}
fn process(&self) -> Option<Process> {
Some(Process {
command: vec!["session".into()],
folder: PathBuf::from("/program"),
})
}
}
#[test]
fn the_live_session_is_one_for_a_served_folder_and_a_document_turns_it_off_or_replaces_it() {
use crate::document::Document;
install_loader(Arc::new(Toy));
let dir = std::env::temp_dir().join(format!("devpage-live-session-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
for folder in ["widgets", "set", "off", "own/model"] {
std::fs::create_dir_all(dir.join(folder)).unwrap();
}
std::fs::write(dir.join("widgets/Mine.tsx"), "export default () => null;").unwrap();
std::fs::write(
dir.join("devpage.yml"),
"kind: site\ntitle: T\nroot: README.md\nwidgets: widgets\nplaces:\n float: [Mine]\n",
)
.unwrap();
std::fs::write(dir.join("README.md"), "# T\n").unwrap();
std::fs::write(dir.join("lone.md"), "# Lone\n").unwrap();
std::fs::write(
dir.join("set/devpage.yml"),
"kind: doc\ntitle: S\nchapters: [a.md]\nextensions:\n live:\n config:\n voice: calm\n",
)
.unwrap();
std::fs::write(dir.join("set/a.md"), "# A\n").unwrap();
std::fs::write(
dir.join("off/devpage.yml"),
"kind: doc\ntitle: O\nchapters: [a.md]\nextensions:\n live:\n config:\n enabled: false\n",
)
.unwrap();
std::fs::write(dir.join("off/a.md"), "# A\n").unwrap();
std::fs::write(
dir.join("own/devpage.yml"),
"kind: doc\ntitle: W\nchapters: [a.md]\nextensions:\n live:\n package: ./package\n config:\n input: model\n",
)
.unwrap();
std::fs::write(dir.join("own/a.md"), "# A\n").unwrap();
let dir = dir.canonicalize().unwrap();
let scope = ServedFolder::new(&dir).unwrap();
let load = |path: &str| Document::load(&dir.join(path), &scope).unwrap();
for path in [
"devpage.yml",
"lone.md",
"set/devpage.yml",
"off/devpage.yml",
] {
let document = load(path);
assert!(document.extensions.is_empty(), "{path}");
assert!(document.places.panel.is_empty(), "{path}");
}
serve_live(&scope, Arc::new(Session));
let site = load("devpage.yml");
let (id, address) = site.extensions[0].address();
assert_eq!(id, LIVE);
assert!(site.extensions[0].is_session());
assert_eq!(site.places.navbar, ["DevpageLiveTasksButton"]);
assert_eq!(site.places.panel, ["DevpageLiveTasks"]);
assert_eq!(
site.places.float,
["Mine", "DevpageLiveCanvas", "DevpageLiveOrb"]
);
for path in ["lone.md", "set/devpage.yml"] {
let document = load(path);
assert_eq!(document.extensions.len(), 1, "{path}");
assert_eq!(document.extensions[0].address().1, address, "{path}");
assert_eq!(document.places.panel, ["DevpageLiveTasks"], "{path}");
}
let asked = live(&format!("{address}live/socket"), &scope).unwrap();
assert_eq!(asked.folder, dir);
assert_eq!(asked.rest, "socket");
assert!(asked.process.is_some());
assert!(bound(&asked.key));
let configured = for_document(&dir.join("set/devpage.yml"));
assert_eq!(
configured[0].0.lock().unwrap().config,
json!({"voice": "calm"})
);
let off = load("off/devpage.yml");
assert!(off.extensions.is_empty());
assert_eq!(off.places, Default::default());
let own = load("own/devpage.yml");
assert_eq!(own.extensions.len(), 1);
assert!(!own.extensions[0].is_session());
assert_ne!(own.extensions[0].address().1, address);
assert_eq!(own.places, Default::default());
std::fs::write(
dir.join("off/devpage.yml"),
"kind: doc\ntitle: O\nchapters: [a.md]\nextensions:\n live:\n config:\n enabled: sometimes\n",
)
.unwrap();
let error = Document::load(&dir.join("off/devpage.yml"), &scope).unwrap_err();
assert!(
format!("{error:#}")
.ends_with("extensions: live: config: enabled is not true or false"),
"{error:#}"
);
std::fs::remove_dir_all(dir).unwrap();
}
}