feat: per-tool save exports + file⇄yjs round-trip tests (0004 §A, phases 2–4)

Block A save embind exports + the round-trip integration harness.

Save exports (wasm/bindings, GPL):
- pcbnew: kicadSaveBoard(path) via PCB_IO_KICAD_SEXPR::SaveBoard(GetBoard()).
- eeschema: kicadSaveSchematic(path) via SCH_IO_KICAD_SEXPR::SaveSchematicFile.
  Saves GetCurrentSheet().Last() (NOT Schematic().Root()): the wasm open-flow
  nests the opened doc under an auto-created project root, so Root()'s screen
  holds only a child-sheet symbol, not the loaded items.
  (pl_editor already had kicadSaveDrawingSheet.)

Round-trip harness (tests/kicad/roundtrip.spec.ts):
- load fixture → save (ORIG) → kicadCollabSnapshot → reload (fresh wasm) →
  open empty → kicadCollabApply → save (REGEN) → assert sexprDiff(ORIG,REGEN).equal.
- Two separate pages (extract closed before rebuild opens) so the process-global
  wasm heap frees between boots (pcbnew ~190MB). Open both sides from the same
  filename (eeschema embeds it as the root Sheetfile property). pcbnew boots the
  seeded pcbnew-collab.html (plain pcbnew.html's first-run wizard blocks boot).
- pl_editor + eeschema round trips PASS (lossless). pcbnew is test.fixme with
  tracked apply-coverage findings (footprints/zones not reconstructed; segment
  width + via size lost; fp_text→gr_text) — bridge gaps for follow-up, not test bugs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Gergő Törcsvári 2026-06-10 18:06:33 +02:00
commit 2163c36f28
No known key found for this signature in database
GPG key ID: 8E75F2CDE64E5322
3 changed files with 397 additions and 0 deletions

View file

@ -23,6 +23,8 @@
#include <layer_ids.h>
#include <schematic.h>
#include <sch_edit_frame.h>
#include <sch_io/kicad_sexpr/sch_io_kicad_sexpr.h>
#include <sch_sheet.h>
#include <sch_commit.h>
#include <sch_item.h>
#include <sch_line.h>
@ -682,9 +684,49 @@ std::string kicadCollabGetPos( std::string aId )
}
// Programmatically save the in-memory schematic to a .kicad_sch file, without
// driving the Save As dialog — eeschema's analogue of pl_editor's
// kicadSaveDrawingSheet. Serializes the root sheet via the same SCH_IO_KICAD_SEXPR
// writer eeschema uses, so a test can read the file back from MEMFS and assert the
// file ⇄ Y.Doc round trip (README §A; feature 0004). Single-sheet scope: the round-
// trip fixtures are flat schematics; saving the root sheet writes the whole model.
void kicadSaveSchematic( std::string path )
{
SCH_EDIT_FRAME* fr = schFrame();
if( !fr )
return;
SCHEMATIC& sch = fr->Schematic();
// Save the CURRENT sheet, not Schematic().Root(): in the wasm open flow the
// opened document is displayed as the current sheet but can sit under an
// auto-created project root, so Root()'s own screen holds only a child-sheet
// symbol (not the loaded items). GetCurrentSheet() is the screen the editor is
// actually showing — the one whose items the snapshot/round-trip care about.
SCH_SHEET* sheet = fr->GetCurrentSheet().Last();
if( !sheet )
sheet = &sch.Root();
try
{
SCH_IO_KICAD_SEXPR io;
io.SaveSchematicFile( wxString::FromUTF8( path.c_str() ), sheet, &sch );
}
catch( ... )
{
// Don't abort the wasm runtime on a save failure; the JS caller detects it
// by the file being absent / empty.
}
}
EMSCRIPTEN_BINDINGS(eeschema) {
// Programmatic file open (preferred over UI automation from the web app).
function("kicadOpenFile", &kicadOpenFile);
// Programmatic save of the in-memory schematic (round-trip tests, README §A).
function("kicadSaveSchematic", &kicadSaveSchematic);
// Yjs collaborative bridge entry points (same contract as pl_editor).
function("kicadCollabApply", &kicadCollabApply);
function("kicadCollabSnapshot", &kicadCollabSnapshot);