/// /** * TEST PATTERN: Superstition #56 - Record .key().set() in handlers * * Claim: .key(key).set(value) on Record may fail when: * 1. Creating NEW keys (vs updating existing) * 2. On an initially empty Record * 3. With keys containing hyphens (interpreted as path separators?) * * Error observed: "Value at path value/argument/corrections/0-Technical_Expertise is not an object" */ import { Cell, Default, handler, NAME, pattern, UI } from "commontools"; interface Item { value: string; count: number; } interface Input { // Empty record to test creating new keys emptyRecord: Default, Record>; // Pre-populated record to test updating existing keys populatedRecord: Default< Record, { existing: { value: "preset"; count: 0 } } >; logs: Default; } // ============================================================ // TEST A: .key().set() on EMPTY record with SIMPLE key // ============================================================ const testEmptySimpleKey = handler< unknown, { record: Cell>; logs: Cell } >( (_, { record, logs }) => { try { const key = "simplekey"; record.key(key).set({ value: "test-a", count: 1 }); const msg = `A: SUCCESS - .key("${key}").set() on empty record`; console.log(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } catch (e) { const msg = `A: FAILED - ${e}`; console.error(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } }, ); // ============================================================ // TEST B: .key().set() on EMPTY record with HYPHENATED key // ============================================================ const testEmptyHyphenKey = handler< unknown, { record: Cell>; logs: Cell } >( (_, { record, logs }) => { try { const key = "0-Technical_Expertise"; // The exact key from the superstition record.key(key).set({ value: "test-b", count: 2 }); const msg = `B: SUCCESS - .key("${key}").set() on empty record`; console.log(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } catch (e) { const msg = `B: FAILED - ${e}`; console.error(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } }, ); // ============================================================ // TEST C: .key().set() on POPULATED record (update existing) // ============================================================ const testPopulatedUpdate = handler< unknown, { record: Cell>; logs: Cell } >( (_, { record, logs }) => { try { const key = "existing"; // This key already exists record.key(key).set({ value: "updated", count: 99 }); const msg = `C: SUCCESS - .key("${key}").set() update existing key`; console.log(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } catch (e) { const msg = `C: FAILED - ${e}`; console.error(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } }, ); // ============================================================ // TEST D: .key().set() on POPULATED record (create new) // ============================================================ const testPopulatedNew = handler< unknown, { record: Cell>; logs: Cell } >( (_, { record, logs }) => { try { const key = "new-hyphen-key"; // New key with hyphen record.key(key).set({ value: "test-d", count: 4 }); const msg = `D: SUCCESS - .key("${key}").set() new key on populated record`; console.log(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } catch (e) { const msg = `D: FAILED - ${e}`; console.error(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } }, ); // ============================================================ // TEST E: Spread workaround (should always work) // ============================================================ const testSpreadWorkaround = handler< unknown, { record: Cell>; logs: Cell } >( (_, { record, logs }) => { try { const key = "spread-key"; const current = record.get() ?? {}; record.set({ ...current, [key]: { value: "test-e", count: 5 } }); const msg = `E: SUCCESS - spread workaround with key "${key}"`; console.log(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } catch (e) { const msg = `E: FAILED - ${e}`; console.error(`[#56] ${msg}`); logs.set([...logs.get(), msg]); } }, ); // Clear logs handler const clearLogs = handler }>( (_, { logs }) => { logs.set([]); }, ); export default pattern(({ emptyRecord, populatedRecord, logs }) => { return { [NAME]: "TEST: Record .key().set() #56", [UI]: (

Record .key().set() Test (#56)

Testing if .key(k).set(v) fails on empty Records or with hyphenated keys

{/* Current state display */}
Current State:
emptyRecord: {JSON.stringify(emptyRecord)}
populatedRecord: {JSON.stringify(populatedRecord)}
{/* Test buttons */}

Tests on Empty Record

A: Simple key on empty
B: Hyphen key on empty (the problematic case)

Tests on Populated Record

C: Update existing key
D: New hyphen key on populated

Workaround

E: Spread workaround on empty
{/* Logs */}

Test Results

Clear
{logs.map((log) => (
{log}
))}
{/* Expected results */}
Superstition claims:
  • A & B should FAIL (empty record)
  • B especially should FAIL (hyphenated key)
  • C should PASS (update existing)
  • D might PASS (populated record)
  • E should always PASS (workaround)
), emptyRecord, populatedRecord, logs, }; });