package core_test import ( "context" "encoding/json" "os" "path/filepath" "testing" "github.com/carlosframework/docs/internal/core" ) // fixture loads fixtures/vectors/fragment/.update.bin, a real // Yjs update building a Y.XmlFragment on root "prosemirror" // (fixtures/generate-fragment.mjs's own "trap-containment vectors" // section), plus its expected plain text. // // Not fixtures/data (a bare Y.Text on root "content"): every test in // this file reads content back through (*Handle).Text, which now // reads fragment_text off root "prosemirror" — see // core/src/abi.rs's text_string doc comment. fixtures/data's updates // still apply cleanly (Apply/apply_slice decode and merge regardless // of which root they target) but read back as "" through Text now, // which is exactly the silent-empty failure this whole fix round // exists to stop reintroducing. func fixture(t *testing.T, name string) ([]byte, string) { t.Helper() dir := filepath.Join("..", "..", "fixtures", "vectors", "fragment") update, err := os.ReadFile(filepath.Join(dir, name+".update.bin")) if err != nil { t.Fatalf("reading fixture: %v", err) } raw, err := os.ReadFile(filepath.Join(dir, name+".expected.json")) if err != nil { t.Fatalf("reading expectation: %v", err) } var want struct { Text string `json:"text"` } if err := json.Unmarshal(raw, &want); err != nil { t.Fatalf("parsing expectation: %v", err) } return update, want.Text } func TestAppliesFixturesUnderWazero(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } t.Cleanup(func() { rt.Close(ctx) }) for _, name := range []string{"hello", "unicode", "formatted", "concurrent"} { t.Run(name, func(t *testing.T) { update, want := fixture(t, name) doc, err := rt.NewDoc(ctx, "go-host-test-guid", 9001) if err != nil { t.Fatalf("NewDoc: %v", err) } defer doc.Close(ctx) if err := doc.Apply(ctx, update); err != nil { t.Fatalf("Apply: %v", err) } got, err := doc.Text(ctx) if err != nil { t.Fatalf("Text: %v", err) } if got != want { t.Errorf("got %q, want %q", got, want) } }) } } func TestGarbageUpdateReturnsErrorNotTrap(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } t.Cleanup(func() { rt.Close(ctx) }) doc, err := rt.NewDoc(ctx, "go-host-test-guid", 9001) if err != nil { t.Fatalf("NewDoc: %v", err) } defer doc.Close(ctx) if err := doc.Apply(ctx, []byte{0xff, 0xff, 0xff, 0xff}); err == nil { t.Fatal("expected an error for garbage input, got nil") } // The instance must still be usable: an error is not a trap. update, want := fixture(t, "hello") if err := doc.Apply(ctx, update); err != nil { t.Fatalf("instance unusable after a rejected update: %v", err) } got, _ := doc.Text(ctx) if got != want { t.Errorf("after recovery got %q, want %q", got, want) } } // TestTrappedInstanceIsDiscardedAndRebuilds lives in core_internal_test.go // (package core), not here. Fix-round-1 finding 1: the trap this test uses // fires inside Update::decode_v1 before any RefCell is borrowed, so a host // that simply kept reusing the trapped instance would pass every // public-API-observable check (Text still succeeds, content still // correct) — the one assertion that can actually fail against a host that // skips the discard is a direct check that the module instance itself // changed, which needs unexported access this external test package // doesn't have. This file's tests only cover the other two cases: a clean // success (TestAppliesFixturesUnderWazero) and a clean rejection // (TestGarbageUpdateReturnsErrorNotTrap), both of which the public API is // enough to verify. func TestHandleHTMLRendersFragment(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } defer rt.Close(ctx) h, err := rt.NewDoc(ctx, "html-test", 1) if err != nil { t.Fatalf("NewDoc: %v", err) } defer h.Close(ctx) update, err := os.ReadFile("../../fixtures/vectors/fragment/simple.bin") if err != nil { t.Fatalf("read fixture: %v", err) } if err := h.Apply(ctx, update); err != nil { t.Fatalf("Apply: %v", err) } got, err := h.HTML(ctx) if err != nil { t.Fatalf("HTML: %v", err) } want := "

First paragraph.

Second paragraph.

" if got != want { t.Errorf("HTML() = %q, want %q", got, want) } } // TestNewDocSucceedsAgainstTheCurrentModule is the end-to-end counterpart // to TestCheckABIVersion (core_internal_test.go): that test proves the // version-comparison decision itself is correct in isolation; this one // proves instantiate still calls it and still accepts the module this // package embeds. Fix round 1 on Task 2: the test previously here // (TestABIVersionIsPinnedAtTwo) only called core.New, which never // instantiates a module or reaches the ABI check at all (only NewDoc/ // rebuild do, via Runtime.instantiate) — it passed even with the check // deleted outright. func TestNewDocSucceedsAgainstTheCurrentModule(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } defer rt.Close(ctx) h, err := rt.NewDoc(ctx, "abi-wiring-test", 1) if err != nil { t.Fatalf("NewDoc: %v", err) } defer h.Close(ctx) } // TestSnapshotRoundTripsThroughAFreshHandle proves Snapshot's bytes are a // real Yjs update: applying them into a brand-new, otherwise-empty handle // reproduces the same text the snapshot was taken from (the snapshots // design, D1). func TestSnapshotRoundTripsThroughAFreshHandle(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } defer rt.Close(ctx) h, err := rt.NewDoc(ctx, "snapshot-source", 1) if err != nil { t.Fatalf("NewDoc: %v", err) } defer h.Close(ctx) update, want := fixture(t, "hello") if err := h.Apply(ctx, update); err != nil { t.Fatalf("Apply: %v", err) } snap, err := h.Snapshot(ctx) if err != nil { t.Fatalf("Snapshot: %v", err) } if len(snap) == 0 { t.Fatal("Snapshot returned no bytes") } fresh, err := rt.NewDoc(ctx, "snapshot-target", 2) if err != nil { t.Fatalf("NewDoc fresh: %v", err) } defer fresh.Close(ctx) if err := fresh.Apply(ctx, snap); err != nil { t.Fatalf("Apply snapshot: %v", err) } got, err := fresh.Text(ctx) if err != nil { t.Fatalf("Text: %v", err) } if got != want { t.Errorf("snapshot rebuilt %q, want %q", got, want) } } // TestVerifySnapshotAcceptsItsOwnAndRejectsGarbage exercises all three // outcomes VerifySnapshot returns without an error: a document's own // snapshot verifies true; a validly-decodable snapshot of a genuinely // different document verifies false (return code 1 — the verifier's // primary purpose, distinct from "not even a decodable update"); and // bytes that are not a decodable update at all (ERR_DECODE) also come // back false with no error — a "no" answer, not a failure of the call. // The middle case and the garbage case are different failures of the // candidate and must not be conflated: only the garbage case may ever // legitimately share its outcome with a rejected-but-valid document. func TestVerifySnapshotAcceptsItsOwnAndRejectsGarbage(t *testing.T) { ctx := context.Background() rt, err := core.New(ctx) if err != nil { t.Fatalf("core.New: %v", err) } defer rt.Close(ctx) h, err := rt.NewDoc(ctx, "verify-test", 1) if err != nil { t.Fatalf("NewDoc: %v", err) } defer h.Close(ctx) update, _ := fixture(t, "hello") if err := h.Apply(ctx, update); err != nil { t.Fatalf("Apply: %v", err) } snap, err := h.Snapshot(ctx) if err != nil { t.Fatalf("Snapshot: %v", err) } ok, err := h.VerifySnapshot(ctx, snap) if err != nil { t.Fatalf("VerifySnapshot: %v", err) } if !ok { t.Error("a document refused its own snapshot") } // A genuinely different document's snapshot: a real, validly-decodable // update, but not a snapshot of h. This is not "simple" (also a // fixtures/vectors/fragment .bin, used elsewhere in this file via // os.ReadFile) reused as a second, unrelated document's content — the // point is that decode succeeds and the structural comparison still // says no. other, err := rt.NewDoc(ctx, "verify-test-other", 2) if err != nil { t.Fatalf("NewDoc other: %v", err) } defer other.Close(ctx) otherUpdate, err := os.ReadFile("../../fixtures/vectors/fragment/simple.bin") if err != nil { t.Fatalf("read fixture: %v", err) } if err := other.Apply(ctx, otherUpdate); err != nil { t.Fatalf("Apply other: %v", err) } otherSnap, err := other.Snapshot(ctx) if err != nil { t.Fatalf("Snapshot other: %v", err) } if ok, err := h.VerifySnapshot(ctx, otherSnap); err != nil || ok { t.Errorf("a different document's snapshot verified against h: ok=%v err=%v", ok, err) } if ok, err := h.VerifySnapshot(ctx, []byte{0xff, 0x00}); err != nil || ok { t.Errorf("garbage verified: ok=%v err=%v", ok, err) } }