Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions engine/mcp/preview/agent.mjs

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions engine/mcp/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -99,3 +101,34 @@ func TestAgentInSync(t *testing.T) {
t.Fatal("engine/mcp/preview/agent.mjs is stale: run `npm run build:agent -w packages/preview`")
}
}

// A note only some runs give, such as a pool only some starts reach, is
// reported over many runs with how many gave it.
func TestPreviewJigsawNotesFromEveryRun(t *testing.T) {
needJS(t)
s := server(t)
dir := t.TempDir()
if err := os.CopyFS(dir, os.DirFS("../../examples/jigsaw")); err != nil {
t.Fatal(err)
}
src, err := os.ReadFile(filepath.Join(dir, "dungeon.mms"))
if err != nil {
t.Fatal(err)
}
// Some runs start with a cap, which leads nowhere, so only the others
// reach the halls pool, which lists a vanilla piece the simulation lacks.
text := strings.Replace(string(src), `start = [Piece("start")]`, `start = [Piece("start"), Piece("caps/hall_cap")]`, 1)
text = strings.Replace(text, `Piece("halls/short_hall"),`, `Piece("halls/short_hall"), WeightedElement(1, SinglePoolElement(rigid, minecraft:village/plains/houses/plains_small_house_1, minecraft:empty)),`, 1)
if err := os.WriteFile(filepath.Join(dir, "dungeon.mms"), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
out, failed := call(t, s, "jigsaw", map[string]any{"dir": dir, "runs": 20, "depth": 1})
if failed {
t.Fatal(out)
}
if strings.Contains(out, "has errors") {
t.Fatal(out)
}
// Seed 0 starts with the cap, so before, the report showed none of the halls run's notes.
wantAll(t, out, "Note: minecraft:village/plains/houses/plains_small_house_1 is not one of this project's structure files, so it is never placed here. (in 9 of 20 runs)")
}
13 changes: 10 additions & 3 deletions packages/preview/src/agent/jigsaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ function poolPieces(files: OutputFile[], pools: Iterable<string>): Map<string, s

const boxText = (b: Simulation['bounds']) => `${b[0].join(' ')} to ${b[1].join(' ')}`;

function header(id: string, s: Simulation): string[] {
function header(id: string, s: Simulation, notes = s.notes): string[] {
const out = [`Jigsaw structure ${id}: depth ${s.depth}${s.depth !== s.size ? ` (its size is ${s.size})` : ''}.`];
if (s.notes.length) out.push(...s.notes.map((n) => `Note: ${n}`));
if (notes.length) out.push(...notes.map((n) => `Note: ${n}`));
return out;
}

/** Every run's notes, in the order first seen, saying how many runs gave each when not all did. */
function notesOf(sims: Simulation[]): string[] {
const runs = new Map<string, number>();
for (const s of sims) for (const n of s.notes) runs.set(n, (runs.get(n) ?? 0) + 1);
return [...runs].map(([n, c]) => (c === sims.length ? n : `${n} (in ${c} of ${sims.length} runs)`));
}

/** One seed: every piece, and every jigsaw block that stayed empty. */
function one(id: string, s: Simulation, files: OutputFile[]): string {
const out = header(id, s);
Expand Down Expand Up @@ -96,7 +103,7 @@ function one(id: string, s: Simulation, files: OutputFile[]): string {

/** Many seeds: how often each piece appears, and how the structure's size varies. */
function many(id: string, sims: Simulation[], files: OutputFile[]): string {
const out = header(id, sims[0]);
const out = header(id, sims[0], notesOf(sims));
out.push(`Seeds ${sims[0].seed}..${sims[sims.length - 1].seed} (${sims.length} runs).`);
const failed = sims.filter((s) => s.failed);
if (failed.length) {
Expand Down
Loading