Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137/** Tests for the edit API: reading, setting (including creating keys and appending), deleting, key listing, immutability, and type errors. @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { type StringJsonc, jsoncDelete, jsoncGetValue, jsoncHas, jsoncKeys, jsoncSet, parseJsoncEdit,} from '../dist/final/neutral/index.mjs';
const asJsonc = (source: string,): StringJsonc => source as StringJsonc;
const fixture = (): ReturnType<typeof parseJsoncEdit> => parseJsoncEdit({ source: asJsonc('{\n "a": 1, // c\n "nested": { "b": 2 },\n "list": [10, 20]\n}',), },);
await describe({ name: 'edit API', children: [ describe({ name: 'read', children: [ it({ name: 'jsoncGetValue reads a nested value', fn: async () => { expect(jsoncGetValue({ state: fixture(), path: ['nested', 'b',], },),).toBe(2,); }, },), it({ name: 'jsoncGetValue reads an array element', fn: async () => { expect(jsoncGetValue({ state: fixture(), path: ['list', 1,], },),).toBe(20,); }, },), it({ name: 'jsoncHas distinguishes present from absent', fn: async () => { const state = fixture(); expect(jsoncHas({ state, path: ['a',], },),).toBe(true,); expect(jsoncHas({ state, path: ['missing',], },),).toBe(false,); }, },), it({ name: 'jsoncKeys lists record keys in order', fn: async () => { expect(jsoncKeys({ state: fixture(), path: [], },),).toEqual(['a', 'nested', 'list',],); }, },), it({ name: 'jsoncGetValue throws on a missing path', fn: async () => { expect(() => { jsoncGetValue({ state: fixture(), path: ['nope',], },); },).toThrow('no JSONC node at path',); }, },), ], },), describe({ name: 'set', children: [ it({ name: 'sets an existing value and leaves the original untouched', fn: async () => { const state = fixture(); const next = jsoncSet({ state, path: ['a',], value: 99, },); expect(jsoncGetValue({ state: next, path: ['a',], },),).toBe(99,); expect(jsoncGetValue({ state, path: ['a',], },),).toBe(1,); }, },), it({ name: 'creates a new trailing key', fn: async () => { const next = jsoncSet({ state: fixture(), path: ['fresh',], value: { x: true, }, },); expect(jsoncGetValue({ state: next, path: ['fresh', 'x',], },),).toBe(true,); }, },), it({ name: 'appends to an array at the length index', fn: async () => { const next = jsoncSet({ state: fixture(), path: ['list', 2,], value: 30, },); expect(jsoncGetValue({ state: next, path: ['list',], },),).toEqual([10, 20, 30,],); }, },), it({ name: 'throws when indexing a scalar', fn: async () => { expect(() => { jsoncSet({ state: fixture(), path: ['a', 'b',], value: 1, },); },).toThrow('cannot index',); }, },), ], },), describe({ name: 'delete', children: [ it({ name: 'deletes a record key', fn: async () => { const next = jsoncDelete({ state: fixture(), path: ['a',], },); expect(jsoncHas({ state: next, path: ['a',], },),).toBe(false,); expect(jsoncKeys({ state: next, path: [], },),).toEqual(['nested', 'list',],); }, },), it({ name: 'deletes an array element and shifts the rest', fn: async () => { const next = jsoncDelete({ state: fixture(), path: ['list', 0,], },); expect(jsoncGetValue({ state: next, path: ['list',], },),).toEqual([20,],); }, },), it({ name: 'throws on deleting the document root', fn: async () => { expect(() => { jsoncDelete({ state: fixture(), path: [], },); },).toThrow('document root',); }, },), ], },), ],},);