diff --git a/apps/app/src/core/sheetDocumentOps.ts b/apps/app/src/core/sheetDocumentOps.ts new file mode 100644 index 00000000..5a0d8473 --- /dev/null +++ b/apps/app/src/core/sheetDocumentOps.ts @@ -0,0 +1,31 @@ +import type { CoreOutcome } from '@faunosteo/core-wasm' +import type { + AnatomicalSheetDocument, + LocalizedText, + SheetEmptyInput, +} from '@faunosteo/core-wasm/types' +import { coreRuntime } from './runtime' + +/** + * The sheet document rules that need no loaded sheet (V1-2.5): the document a new sheet + * starts from. The slug is not part of it; it goes to `create_official_sheet` beside it. + */ + +export interface EmptySheetInput { + speciesId: string + name: LocalizedText +} + +/** Failure: `invalid_shape` for an invalid species id or an empty label. */ +export function emptySheetDocument( + input: EmptySheetInput, +): CoreOutcome { + const payload: SheetEmptyInput = { + species_id: input.speciesId, + name: input.name, + } + return coreRuntime().call( + 'sheet.empty', + JSON.stringify(payload), + ) +} diff --git a/apps/app/tests/unit/core.sheetDocumentOps.test.ts b/apps/app/tests/unit/core.sheetDocumentOps.test.ts new file mode 100644 index 00000000..ccdc71b9 --- /dev/null +++ b/apps/app/tests/unit/core.sheetDocumentOps.test.ts @@ -0,0 +1,37 @@ +/** + * V1-2.5: the new sheet's document comes from the core (contract §7.4), through the + * page's runtime; the wrapper maps the input names and passes the closed refusal on. + */ +import { beforeAll, describe, expect, it } from 'vitest' +import { setCoreRuntime } from '@/core/runtime' +import { emptySheetDocument } from '@/core/sheetDocumentOps' +import { testCoreRuntime } from '../helpers/core' + +const name = { fr: 'Nouvelle fiche', en: 'New sheet' } + +beforeAll(async () => { + setCoreRuntime(await testCoreRuntime()) +}) + +describe('emptySheetDocument', () => { + it('answers the empty document of the species, named', () => { + const outcome = emptySheetDocument({ speciesId: 'equine', name }) + expect(outcome.ok).toBe(true) + if (!outcome.ok) return + expect(outcome.value.species_id).toBe('equine') + expect(outcome.value.name).toEqual(name) + expect(outcome.value.views.map((view) => view.id)).toEqual(['view-main']) + }) + + it('refuses an invalid species id or an empty label', () => { + for (const input of [ + { speciesId: 'Not An Id', name }, + { speciesId: 'equine', name: { fr: name.fr, en: '' } }, + ]) { + expect(emptySheetDocument(input)).toMatchObject({ + ok: false, + kind: 'invalid_shape', + }) + } + }) +}) diff --git a/crates/core/src/ops.rs b/crates/core/src/ops.rs index f01ee13d..7ac9ac43 100644 --- a/crates/core/src/ops.rs +++ b/crates/core/src/ops.rs @@ -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 { 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)] diff --git a/crates/core/tests/sheet_rules.rs b/crates/core/tests/sheet_rules.rs index c0d75f2e..795dd957 100644 --- a/crates/core/tests/sheet_rules.rs +++ b/crates/core/tests/sheet_rules.rs @@ -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 diff --git a/docs/development/RUST.md b/docs/development/RUST.md index f4cba3c5..94e3a434 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3873,7 +3873,8 @@ marking is written `inherited`): `general` is the document without `views`, `ann Free (`call`): `sheet.edit` `{document, command}` → `{document}`, failures `unknown_key`, `duplicate_key`, `invalid_target`, `id_changed`, `invalid_position`, `too_deep`, `has_dependents` (§12.7), `too_large` (input over 8 MiB + 2 MiB or the OD-3 value -budget); `sheet.dependents` `{document, key}` → `{dependents: [{element, relation}]}`. Wasm classes: `PreviewBuilder` +budget); `sheet.dependents` `{document, key}` → `{dependents: [{element, relation}]}`; `sheet.empty` (V1-2.5) +`{species_id, name: {fr, en}}` → the empty document (`create_empty_sheet_document`, no slug), failure `invalid_shape`. Wasm classes: `PreviewBuilder` (`add_drawing(assetId, sha256, bytes)`, `build(PreviewInput JSON)` throwing `: `: `invalid_document`, `drawing_missing`, `drawing_hash_mismatch`, `fingerprint_mismatch`, `caps`), `PreviewSheet` (`call`: `preview.state`, `view.canvas`; `replace_document`, `add_drawing`: `{"ok":true,"value":null}` or `invalid_document`, diff --git a/packages/core-wasm/tests/preview.test.ts b/packages/core-wasm/tests/preview.test.ts index c6a92122..395183c7 100644 --- a/packages/core-wasm/tests/preview.test.ts +++ b/packages/core-wasm/tests/preview.test.ts @@ -2,8 +2,8 @@ * V1-2.2 on the shipped module (contract §8.1, §9.2, §10): the equine draft previews with the * index, the fingerprint and the canvases of the bundled sheet; hash and fingerprint * mismatches refuse the build with their closed kind; a draft's document is replaced and - * redrawn; the edit commands run through `call`; every hostile SVG and flood gets a closed - * cleaning answer with no trap. The owner's decisions of 2026-10-29 too: canvases scope their + * redrawn; the edit commands and the empty document run through `call`; every hostile SVG + * and flood gets a closed cleaning answer with no trap. The owner's decisions of 2026-10-29 too: canvases scope their * ids per view, dangling references and removals that leave a dependent are refused, the * stored form is what a file uploads, cairo's `rgb()` colours are kept. The Rust suites hold * the rules; this proves the facade. @@ -137,6 +137,21 @@ describe('PreviewSheet through the shipped module', () => { ) expect(named.kind).toBe('has_dependents') }) + + it('answers the empty document of a new sheet, refusals closed', () => { + const name = { fr: 'Nouvelle fiche', en: 'New sheet' } + const document = value(glue.call('sheet.empty', JSON.stringify({ species_id: 'equine', name }))) + expect(document).toMatchObject({ species_id: 'equine', name, annotations: [] }) + const check = value(glue.call('sheet.check', JSON.stringify(document))) + expect(check.issues.map((issue: { code: string }) => issue.code)).toEqual(['view.asset.missing']) + for (const input of [ + { species_id: 'Not An Id', name }, + { species_id: 'equine', name: { fr: '', en: 'New sheet' } }, + { species_id: 'equine', name, slug: 'equine' }, + ]) { + expect(JSON.parse(glue.call('sheet.empty', JSON.stringify(input))).kind).toBe('invalid_shape') + } + }) }) describe('clean_svg through the shipped module', () => { diff --git a/packages/core-wasm/types/SheetEmptyInput.ts b/packages/core-wasm/types/SheetEmptyInput.ts new file mode 100644 index 00000000..57dbd0b9 --- /dev/null +++ b/packages/core-wasm/types/SheetEmptyInput.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { LocalizedText } from "./LocalizedText"; + +/** + * A new sheet's starting point (V1-2 contract §7.4): the slug is not part of the document. + */ +export type SheetEmptyInput = { species_id: string, name: LocalizedText, }; diff --git a/packages/core-wasm/types/index.ts b/packages/core-wasm/types/index.ts index 2240537f..3eae1ad5 100644 --- a/packages/core-wasm/types/index.ts +++ b/packages/core-wasm/types/index.ts @@ -99,6 +99,7 @@ export type { SheetDependents } from './SheetDependents' export type { SheetDependentsInput } from './SheetDependentsInput' export type { SheetEditInput } from './SheetEditInput' export type { SheetEditOutput } from './SheetEditOutput' +export type { SheetEmptyInput } from './SheetEmptyInput' export type { SheetIndexData } from './SheetIndexData' export type { SheetIssue } from './SheetIssue' export type { SheetIssueSeverity } from './SheetIssueSeverity'