feat(domain): sheet.empty free op for a new sheet's document (V1-2.5 C)
Wraps create_empty_sheet_document; refusals invalid_shape / invalid_json; ts-rs SheetEmptyInput, core-wasm proof, app wrapper emptySheetDocument.
This commit is contained in:
1 parent
b0a4cbe588
commit
bbab0e61cb
8 files changed
+159
-4
No files matched your search
+20
-1
@@ -22,7 +22,8 @@ use crate::domain::sheet::loaded::Drawing;
|
||||
use crate::domain::sheet::preview::PreviewSheet;
|
||||
use crate::domain::sheet::verdict::MAX_DOCUMENT_TEXT_BYTES;
|
||||
use crate::domain::sheet::{
|
||||
AnatomicalSheetDocument, LoadedSheet, SheetIndexData, check_sheet_document,
|
||||
AnatomicalSheetDocument, LoadedSheet, LocalizedText, SheetIndexData, check_sheet_document,
|
||||
create_empty_sheet_document,
|
||||
};
|
||||
use crate::facade::{Failure, parse_input, to_value};
|
||||
use crate::pdf::json_js::{MAX_JSON_VALUES, prescan};
|
||||
@@ -105,6 +106,7 @@ pub struct VertebraSelectInput {
|
||||
pub fn free(op: &str, input: &str) -> Option<Outcome> {
|
||||
Some(match op {
|
||||
"sheet.check" => sheet_check(input),
|
||||
"sheet.empty" => sheet_empty(input),
|
||||
"report.new" => report_new(input),
|
||||
"report.add_observation" => report_add(input),
|
||||
"report.remove_observation" => report_remove(input),
|
||||
@@ -123,6 +125,23 @@ fn sheet_check(input: &str) -> Outcome {
|
||||
to_value(&check)
|
||||
}
|
||||
|
||||
/// A new sheet's starting point (V1-2 contract §7.4): the slug is not part of the document.
|
||||
#[derive(Deserialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct SheetEmptyInput {
|
||||
pub species_id: String,
|
||||
pub name: LocalizedText,
|
||||
}
|
||||
|
||||
/// An empty label is refused by the input's shape, an invalid species id by the document's.
|
||||
fn sheet_empty(input: &str) -> Outcome {
|
||||
let input: SheetEmptyInput = parse_input(input)?;
|
||||
create_empty_sheet_document(&input.species_id, &input.name)
|
||||
.map_err(|error| Failure::new("invalid_shape", error))
|
||||
.and_then(|document| to_value(&document))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
#[serde(deny_unknown_fields)]
|
||||
|
||||
@@ -10,6 +10,7 @@ use faunosteo_core::domain::sheet::{
|
||||
SheetIssueSeverity, SheetView, check_sheet_document, create_empty_sheet_document,
|
||||
is_publishable, validate_sheet_document,
|
||||
};
|
||||
use faunosteo_core::facade::call;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const MAIN_ASSET: &str = "3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8";
|
||||
@@ -476,6 +477,49 @@ fn the_empty_document_is_shape_valid_but_not_publishable() {
|
||||
assert!(create_empty_sheet_document("Not An Id", &name).is_err());
|
||||
}
|
||||
|
||||
fn sheet_empty(input: Value) -> Value {
|
||||
serde_json::from_str(&call("sheet.empty", &input.to_string())).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sheet_empty_answers_the_document_sheet_check_reads() {
|
||||
let name = json!({ "fr": "Nouvelle fiche", "en": "New sheet" });
|
||||
let out = sheet_empty(json!({ "species_id": "equine", "name": name }));
|
||||
assert_eq!(out["ok"], json!(true), "{out}");
|
||||
let document = out["value"].clone();
|
||||
assert_eq!(document["species_id"], json!("equine"));
|
||||
assert_eq!(document["name"], name);
|
||||
assert!(document.get("slug").is_none());
|
||||
let check: Value = serde_json::from_str(&call("sheet.check", &document.to_string())).unwrap();
|
||||
assert_eq!(check["ok"], json!(true), "{check}");
|
||||
let codes: Vec<&Value> = check["value"]["issues"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|issue| &issue["code"])
|
||||
.collect();
|
||||
assert_eq!(codes, [&json!("view.asset.missing")]);
|
||||
assert_eq!(check["value"]["publishable"], json!(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sheet_empty_refuses_a_bad_species_an_empty_label_or_an_unknown_field() {
|
||||
let name = json!({ "fr": "Nouvelle fiche", "en": "New sheet" });
|
||||
for input in [
|
||||
json!({ "species_id": "Not An Id", "name": name }),
|
||||
json!({ "species_id": "equine", "name": { "fr": "", "en": "New sheet" } }),
|
||||
json!({ "species_id": "equine", "name": { "fr": "Nouvelle fiche" } }),
|
||||
json!({ "species_id": "equine", "name": name, "slug": "equine" }),
|
||||
json!({ "name": name }),
|
||||
] {
|
||||
let out = sheet_empty(input.clone());
|
||||
assert_eq!(out["ok"], json!(false), "{input}");
|
||||
assert_eq!(out["kind"], json!("invalid_shape"), "{input}");
|
||||
}
|
||||
let out: Value = serde_json::from_str(&call("sheet.empty", "{ not json")).unwrap();
|
||||
assert_eq!(out["kind"], json!("invalid_json"));
|
||||
}
|
||||
|
||||
/// True for a `{ fr?, en? }` label object, the only thing a translator edits.
|
||||
fn is_label(value: &Value) -> bool {
|
||||
value
|
||||
|
||||
Reference in new issue
Block a user