File size: 11,698 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { describe, it, expect } from "vitest";
import {
  generatePattern,
  validatePattern,
  scorePattern,
  cellKey,
  type PatternCell,
  type PatternDifficulty,
  type Shape,
  type Fill,
  type Size,
  type Rotation,
} from "../patterns";

// --- Helpers ---

const VALID_SHAPES: Shape[] = ["circle", "triangle", "square", "diamond", "star", "hexagon"];
const VALID_FILLS: Fill[] = ["solid", "striped", "empty"];
const VALID_SIZES: Size[] = ["small", "medium", "large"];
const VALID_ROTATIONS: Rotation[] = [0, 45, 90, 135, 180, 225, 270, 315];

function isValidCell(cell: PatternCell): boolean {
  return (
    VALID_SHAPES.includes(cell.shape) &&
    VALID_FILLS.includes(cell.fill) &&
    VALID_SIZES.includes(cell.size) &&
    VALID_ROTATIONS.includes(cell.rotation)
  );
}

// --- cellKey ---

describe("cellKey", () => {
  it("produces a deterministic string from cell attributes", () => {
    const cell: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    expect(cellKey(cell)).toBe("circle-solid-small-0");
  });

  it("produces different keys for different cells", () => {
    const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    const b: PatternCell = { shape: "triangle", fill: "solid", size: "small", rotation: 0 };
    expect(cellKey(a)).not.toBe(cellKey(b));
  });

  it("produces same key for identical cells", () => {
    const a: PatternCell = { shape: "star", fill: "striped", size: "large", rotation: 90 };
    const b: PatternCell = { shape: "star", fill: "striped", size: "large", rotation: 90 };
    expect(cellKey(a)).toBe(cellKey(b));
  });
});

// --- validatePattern ---

describe("validatePattern", () => {
  it("returns true for matching cells", () => {
    const cell: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    expect(validatePattern(cell, cell)).toBe(true);
  });

  it("returns false for different shapes", () => {
    const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    const b: PatternCell = { shape: "square", fill: "solid", size: "small", rotation: 0 };
    expect(validatePattern(a, b)).toBe(false);
  });

  it("returns false for different fills", () => {
    const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    const b: PatternCell = { shape: "circle", fill: "empty", size: "small", rotation: 0 };
    expect(validatePattern(a, b)).toBe(false);
  });

  it("returns false for different sizes", () => {
    const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    const b: PatternCell = { shape: "circle", fill: "solid", size: "large", rotation: 0 };
    expect(validatePattern(a, b)).toBe(false);
  });

  it("returns false for different rotations", () => {
    const a: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 0 };
    const b: PatternCell = { shape: "circle", fill: "solid", size: "small", rotation: 90 };
    expect(validatePattern(a, b)).toBe(false);
  });
});

// --- scorePattern ---

describe("scorePattern", () => {
  it("returns 1.0 for correct answer", () => {
    const cell: PatternCell = { shape: "diamond", fill: "striped", size: "medium", rotation: 45 };
    expect(scorePattern(cell, cell)).toBe(1.0);
  });

  it("returns 0.0 for wrong answer", () => {
    const a: PatternCell = { shape: "diamond", fill: "striped", size: "medium", rotation: 45 };
    const b: PatternCell = { shape: "hexagon", fill: "striped", size: "medium", rotation: 45 };
    expect(scorePattern(a, b)).toBe(0.0);
  });
});

// --- generatePattern: structure ---

describe("generatePattern - structure", () => {
  const difficulties: PatternDifficulty[] = ["easy", "medium", "hard"];

  for (const diff of difficulties) {
    describe(`difficulty=${diff}`, () => {
      // Generate a few puzzles to test stochastic properties
      const puzzles = Array.from({ length: 10 }, () => generatePattern(diff));

      it("returns a grid of 9 elements", () => {
        for (const p of puzzles) {
          expect(p.grid).toHaveLength(9);
        }
      });

      it("has exactly one null cell (index 8)", () => {
        for (const p of puzzles) {
          expect(p.grid[8]).toBeNull();
          const nullCount = p.grid.filter((c) => c === null).length;
          expect(nullCount).toBe(1);
        }
      });

      it("has valid cells for all non-null grid positions", () => {
        for (const p of puzzles) {
          for (let i = 0; i < 8; i++) {
            const cell = p.grid[i];
            expect(cell).not.toBeNull();
            expect(isValidCell(cell!)).toBe(true);
          }
        }
      });

      it("answer is a valid cell", () => {
        for (const p of puzzles) {
          expect(isValidCell(p.answer)).toBe(true);
        }
      });

      it("correctIndex points to the answer in choices", () => {
        for (const p of puzzles) {
          const chosen = p.choices[p.correctIndex];
          expect(cellKey(chosen)).toBe(cellKey(p.answer));
        }
      });

      it("all choices are valid cells", () => {
        for (const p of puzzles) {
          for (const choice of p.choices) {
            expect(isValidCell(choice)).toBe(true);
          }
        }
      });

      it("choices have no duplicates", () => {
        for (const p of puzzles) {
          const keys = p.choices.map(cellKey);
          expect(new Set(keys).size).toBe(keys.length);
        }
      });

      it("difficulty matches input", () => {
        for (const p of puzzles) {
          expect(p.difficulty).toBe(diff);
        }
      });

      it("has a non-empty hint string", () => {
        for (const p of puzzles) {
          expect(p.hint.length).toBeGreaterThan(0);
        }
      });

      it("activeAttributes is non-empty", () => {
        for (const p of puzzles) {
          expect(p.activeAttributes.length).toBeGreaterThan(0);
        }
      });
    });
  }
});

// --- generatePattern: difficulty scaling ---

describe("generatePattern - difficulty scaling", () => {
  it("easy puzzles have 2 active attributes", () => {
    const puzzles = Array.from({ length: 20 }, () => generatePattern("easy"));
    for (const p of puzzles) {
      expect(p.activeAttributes).toHaveLength(2);
    }
  });

  it("medium puzzles have 3 active attributes", () => {
    const puzzles = Array.from({ length: 20 }, () => generatePattern("medium"));
    for (const p of puzzles) {
      expect(p.activeAttributes).toHaveLength(3);
    }
  });

  it("hard puzzles have 4 active attributes", () => {
    const puzzles = Array.from({ length: 20 }, () => generatePattern("hard"));
    for (const p of puzzles) {
      expect(p.activeAttributes).toHaveLength(4);
    }
  });

  it("easy puzzles have 4 choices", () => {
    const puzzles = Array.from({ length: 10 }, () => generatePattern("easy"));
    for (const p of puzzles) {
      expect(p.choices).toHaveLength(4);
    }
  });

  it("medium puzzles have 5 choices", () => {
    const puzzles = Array.from({ length: 10 }, () => generatePattern("medium"));
    for (const p of puzzles) {
      expect(p.choices).toHaveLength(5);
    }
  });

  it("hard puzzles have 6 choices", () => {
    const puzzles = Array.from({ length: 10 }, () => generatePattern("hard"));
    for (const p of puzzles) {
      expect(p.choices).toHaveLength(6);
    }
  });
});

// --- generatePattern: pattern integrity ---

describe("generatePattern - pattern integrity", () => {
  it("row-shift rule produces correct shifting pattern", () => {
    // Generate many puzzles and check that at least some use row-shift
    // where each row is a rotation of the same triad
    const puzzles = Array.from({ length: 50 }, () => generatePattern("medium"));

    // For each puzzle, verify the answer (cell 8) is derivable from the grid
    // by checking that the answer + cell 8's row/col contexts are consistent
    for (const p of puzzles) {
      // The full grid (with answer placed back) should have consistent patterns
      const full = [...p.grid.slice(0, 8), p.answer];

      // Check: each row should have internally consistent attributes
      for (let row = 0; row < 3; row++) {
        const rowCells = [full[row * 3], full[row * 3 + 1], full[row * 3 + 2]];
        // All cells should exist
        expect(rowCells.every((c) => c !== null)).toBe(true);
      }

      // Check: each column should have internally consistent attributes
      for (let col = 0; col < 3; col++) {
        const colCells = [full[col], full[3 + col], full[6 + col]];
        expect(colCells.every((c) => c !== null)).toBe(true);
      }
    }
  });

  it("same rule produces identical values across rows", () => {
    // Generate many easy puzzles — easy mode preferentially uses "same" rule
    const puzzles = Array.from({ length: 100 }, () => generatePattern("easy"));

    let foundSameRule = false;
    for (const p of puzzles) {
      const full = [...p.grid.slice(0, 8), p.answer];

      // Check each attribute: if all 3 values in a row are identical for all 3 rows,
      // that attribute uses the "same" rule
      for (const attr of ["shape", "fill", "size", "rotation"] as const) {
        let allRowsSame = true;
        for (let row = 0; row < 3; row++) {
          const vals = [
            full[row * 3]![attr],
            full[row * 3 + 1]![attr],
            full[row * 3 + 2]![attr],
          ];
          if (vals[0] !== vals[1] || vals[1] !== vals[2]) {
            allRowsSame = false;
            break;
          }
        }
        if (allRowsSame) {
          foundSameRule = true;
        }
      }
    }

    // With 100 easy puzzles, extremely likely to find at least one "same" rule
    expect(foundSameRule).toBe(true);
  });
});

// --- generatePattern: active attributes coverage ---

describe("generatePattern - attribute coverage", () => {
  it("all 4 attributes appear across many puzzles", () => {
    const seen = new Set<string>();
    const puzzles = Array.from({ length: 50 }, () => generatePattern("hard"));
    for (const p of puzzles) {
      for (const attr of p.activeAttributes) {
        seen.add(attr);
      }
    }
    expect(seen).toContain("shape");
    expect(seen).toContain("fill");
    expect(seen).toContain("size");
    expect(seen).toContain("rotation");
  });

  it("hard puzzles always include all 4 attributes", () => {
    const puzzles = Array.from({ length: 20 }, () => generatePattern("hard"));
    for (const p of puzzles) {
      const attrs = new Set(p.activeAttributes);
      expect(attrs.has("shape")).toBe(true);
      expect(attrs.has("fill")).toBe(true);
      expect(attrs.has("size")).toBe(true);
      expect(attrs.has("rotation")).toBe(true);
    }
  });
});

// --- generatePattern: variety ---

describe("generatePattern - variety", () => {
  it("generates different puzzles (non-deterministic)", () => {
    const keys = new Set<string>();
    for (let i = 0; i < 20; i++) {
      const p = generatePattern("medium");
      // Use grid cell keys as a puzzle fingerprint
      const fingerprint = p.grid
        .map((c) => (c ? cellKey(c) : "null"))
        .join("|");
      keys.add(fingerprint);
    }
    // Should have at least 2 distinct puzzles out of 20
    expect(keys.size).toBeGreaterThan(1);
  });

  it("uses multiple shape types across puzzles", () => {
    const shapes = new Set<string>();
    for (let i = 0; i < 30; i++) {
      const p = generatePattern("medium");
      for (const cell of p.grid) {
        if (cell) shapes.add(cell.shape);
      }
    }
    // Should see at least 3 different shapes
    expect(shapes.size).toBeGreaterThanOrEqual(3);
  });
});