cultcache-ts: registerIndex refuses a name the definition declares as a role
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5e610b1b6e
commit
8a070e0dd7
3 changed files with 23 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import {
|
||||
declaresRole,
|
||||
decodeDocumentPayload,
|
||||
documentPayloadBytes,
|
||||
documentPayloadFromBytes,
|
||||
|
|
@ -240,6 +241,10 @@ export class CultCache {
|
|||
throw new Error(`CultCache index names for type "${definition.type}" must be non-empty.`);
|
||||
}
|
||||
|
||||
if (declaresRole(definition, indexName)) {
|
||||
throw new Error(`Index "${indexName}" of ${definition.type} is declared as a role; a role is never a unique index.`);
|
||||
}
|
||||
|
||||
const registered = this.#requireDefinition(definition);
|
||||
const indexAccessors = new Map(registered.indexAccessors);
|
||||
indexAccessors.set(indexName, this.#compileAccessor(definition.type, `index "${indexName}"`, accessor));
|
||||
|
|
|
|||
|
|
@ -131,6 +131,12 @@ export function encodeDocumentPayload(definition: AnyCultCacheDocumentDefinition
|
|||
return documentPayloadFromBytes(bytes);
|
||||
}
|
||||
|
||||
// Whether the definition declares `name` as a role alias (either `roles` form).
|
||||
export function declaresRole(definition: AnyCultCacheDocumentDefinition, name: string): boolean {
|
||||
const roles = definition.roles;
|
||||
return Array.isArray(roles) ? roles.some((entry) => entry.name === name) : roles ? Object.hasOwn(roles, name) : false;
|
||||
}
|
||||
|
||||
// The schema members of a document: the ones it declares, else those its codec derives, else none.
|
||||
export function resolveDocumentMembers(definition: AnyCultCacheDocumentDefinition): readonly CultCacheSchemaCatalogMember[] {
|
||||
if (definition.members || !definition.codec) {
|
||||
|
|
|
|||
|
|
@ -381,11 +381,21 @@ test("a refused load leaves the cache's prior state as it was, and a later good
|
|||
}
|
||||
});
|
||||
|
||||
test("registerIndex over held duplicates installs nothing", async () => {
|
||||
test("registerIndex refuses a name the definition declares as a role", async () => {
|
||||
const document = item("item", "roles");
|
||||
const cache = memory(document);
|
||||
await cache.put(document, "a", { sku: "S", group: "g" });
|
||||
await cache.put(document, "b", { sku: "S", group: "h" });
|
||||
await assert.rejects(cache.registerIndex(document, "sku", "sku"), { message: /declared as a role; a role is never a unique index/u });
|
||||
assert.equal(cache.getKeyByIndex(document, "sku", "S"), undefined);
|
||||
await cache.put(document, "c", { sku: "S", group: "i" });
|
||||
});
|
||||
|
||||
test("registerIndex over held duplicates installs nothing", async () => {
|
||||
const document = item("item", "none");
|
||||
const cache = memory(document);
|
||||
await cache.put(document, "a", { sku: "S", group: "g" });
|
||||
await cache.put(document, "b", { sku: "S", group: "h" });
|
||||
await assert.rejects(cache.registerIndex(document, "sku", "sku"), { message: refusal("a", "b", "tests.item", "sku") });
|
||||
assert.equal(cache.getKeyByIndex(document, "sku", "S"), undefined, "no map was installed");
|
||||
await cache.put(document, "c", { sku: "S", group: "i" });
|
||||
|
|
@ -468,7 +478,7 @@ test("a load judges every unique alias: a store collision on the second alias re
|
|||
});
|
||||
|
||||
test("registerIndex of a second unique alias judges it, naming the two smallest keys whatever order they were written in", async () => {
|
||||
const document = pair({ indexes: { a: "a" }, roles: { b: "b" } });
|
||||
const document = pair({ indexes: { a: "a" } });
|
||||
const cache = memory(document);
|
||||
await cache.put(document, "z", { a: "1", b: "s" });
|
||||
await cache.put(document, "m", { a: "2", b: "s" });
|
||||
|
|
|
|||
Loading…
Reference in a new issue