CodeSampleX

Ejemplo

lru-cache 5.1.1

Muestra verificada para npm lru-cache 5.1.1. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó: LRUCache get and set store and retrieve…

sha256:33f47a04a5ae9b9d961ce0a162da870061652a08dbe1a0d76c4ecc3e2e425ad1

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-21

Caso

HOW
Objetivo
verify pkg:npm/lru-cache@5.1.1
Paquetes
Creado
2026-08-21T13:48:28Z

Contrato

  1. LRUCache get and set store and retrieve key-value pairs while respecting maximum capacity by evicting least recently used entries
  2. LRUCache peek inspects cached values without updating the recency order
  3. LRUCache has and del verify existence and delete individual entries from the cache
  4. LRUCache forEach and rforEach iterate entries in most-recently-used and least-recently-used order
  5. LRUCache keys and values return arrays of keys and values in order from newest to oldest
  6. LRUCache dump and load serialize the cache state and restore entries into a separate cache instance
  7. LRUCache dispose callback is triggered when an entry is evicted due to capacity or deleted
  8. LRUCache reset removes all entries and resets cache length and itemCount to 0

Archivos

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:npm/lru-cache@5.1.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/lru-cache@5.1.1

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:558b001db97565e3e2b614aba841cd60010bd2bea8bd45a016a2b0f191534611","contract":["LRUCache get and set store and retrieve key-value pairs while respecting maximum capacity by evicting least recently used entries","LRUCache peek inspects cached values without updating the recency order","LRUCache has and del verify existence and delete individual entries from the cache","LRUCache forEach and rforEach iterate entries in most-recently-used and least-recently-used order","LRUCache keys and values return arrays of keys and values in order from newest to oldest","LRUCache dump and load serialize the cache state and restore entries into a separate cache instance","LRUCache dispose callback is triggered when an entry is evicted due to capacity or deleted","LRUCache reset removes all entries and resets cache length and itemCount to 0"],"goal":"verify pkg:npm/lru-cache@5.1.1","kind":"HOW","packages":["pkg:npm/lru-cache@5.1.1"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/lru-cache@5.1.1"],"schemaVersion":1,"subject":"pkg:npm/lru-cache@5.1.1","verifierAdapter":"node-typescript@1"}
index.mjs
import LRU from "lru-cache";

export function createCache(options) {
  return new LRU(options);
}
package-lock.json
{
  "name": "sample-lru-cache",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-lru-cache",
      "version": "1.0.0",
      "dependencies": {
        "lru-cache": "5.1.1"
      }
    },
    "node_modules/lru-cache": {
      "version": "5.1.1",
      "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
      "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==",
      "license": "ISC",
      "dependencies": {
        "yallist": "^3.0.2"
      }
    },
    "node_modules/yallist": {
      "version": "3.1.1",
      "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
      "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==",
      "license": "ISC"
    }
  }
}
package.json
{
  "name": "sample-lru-cache",
  "version": "1.0.0",
  "description": "Clean-room verification sample for lru-cache",
  "type": "module",
  "private": true,
  "dependencies": {
    "lru-cache": "5.1.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/lru-cache@5.1.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/lru-cache@5.1.1"
  ]
}
test/contract.mjs
import assert from "node:assert/strict";
import LRU from "lru-cache";
import { createCache } from "../index.mjs";

// 1. LRUCache get and set store and retrieve key-value pairs while respecting maximum capacity by evicting least recently used entries
{
  const cache = createCache({ max: 3 });
  cache.set("a", 1);
  cache.set("b", 2);
  cache.set("c", 3);

  assert.equal(cache.get("a"), 1);
  assert.equal(cache.get("b"), 2);
  assert.equal(cache.get("c"), 3);
  assert.equal(cache.itemCount, 3);

  // Access "a" so "b" becomes least recently used: c -> a -> b (LRU is b)
  cache.get("a");
  cache.set("d", 4);
  assert.equal(cache.get("b"), undefined);
  assert.equal(cache.get("a"), 1);
  assert.equal(cache.get("c"), 3);
  assert.equal(cache.get("d"), 4);
  assert.equal(cache.itemCount, 3);
}

// 2. LRUCache peek inspects cached values without updating the recency order
{
  const cache = new LRU({ max: 2 });
  cache.set("first", "val1");
  cache.set("second", "val2");

  // Peek "first" without making it most recent
  assert.equal(cache.peek("first"), "val1");

  // Adding "third" should evict "first" because peek did not refresh "first"
  cache.set("third", "val3");
  assert.equal(cache.get("first"), undefined);
  assert.equal(cache.get("second"), "val2");
  assert.equal(cache.get("third"), "val3");
}

// 3. LRUCache has and del verify existence and delete individual entries from the cache
{
  const cache = new LRU({ max: 5 });
  cache.set("k1", "v1");
  cache.set("k2", "v2");

  assert.equal(cache.has("k1"), true);
  assert.equal(cache.has("k3"), false);

  cache.del("k1");
  assert.equal(cache.has("k1"), false);
  assert.equal(cache.get("k1"), undefined);
  assert.equal(cache.itemCount, 1);
}

// 4. LRUCache forEach and rforEach iterate entries in most-recently-used and least-recently-used order
{
  const cache = new LRU({ max: 5 });
  cache.set("x", 10);
  cache.set("y", 20);
  cache.set("z", 30);
  // Order newest to oldest: z, y, x

  const forward = [];
  cache.forEach((val, key) => {
    forward.push({ key, val });
  });
  assert.deepEqual(forward, [
    { key: "z", val: 30 },
    { key: "y", val: 20 },
    { key: "x", val: 10 }
  ]);

  const reverse = [];
  cache.rforEach((val, key) => {
    reverse.push({ key, val });
  });
  assert.deepEqual(reverse, [
    { key: "x", val: 10 },
    { key: "y", val: 20 },
    { key: "z", val: 30 }
  ]);
}

// 5. LRUCache keys and values return arrays of keys and values in order from newest to oldest
{
  const cache = new LRU({ max: 3 });
  cache.set("alpha", 1);
  cache.set("beta", 2);
  cache.set("gamma", 3);

  // Access alpha to make it newest: beta -> gamma -> alpha
  cache.get("alpha");

  assert.deepEqual(cache.keys(), ["alpha", "gamma", "beta"]);
  assert.deepEqual(cache.values(), [1, 3, 2]);
}

// 6. LRUCache dump and load serialize the cache state and restore entries into a separate cache instance
{
  const original = new LRU({ max: 3 });
  original.set("p", 100);
  original.set("q", 200);

  const serialized = original.dump();
  assert.equal(Array.isArray(serialized), true);
  assert.equal(serialized.length, 2);

  const restored = new LRU({ max: 3 });
  restored.load(serialized);

  assert.equal(restored.get("p"), 100);
  assert.equal(restored.get("q"), 200);
  assert.deepEqual(restored.keys(), original.keys());
}

// 7. LRUCache dispose callback is triggered when an entry is evicted due to capacity or deleted
{
  const disposed = [];
  const cache = new LRU({
    max: 2,
    dispose: (key, val) => {
      disposed.push({ key, val });
    }
  });

  cache.set("e1", "val1");
  cache.set("e2", "val2");
  cache.set("e3", "val3"); // e1 is evicted

  assert.deepEqual(disposed, [{ key: "e1", val: "val1" }]);

  cache.del("e2");
  assert.deepEqual(disposed, [
    { key: "e1", val: "val1" },
    { key: "e2", val: "val2" }
  ]);
}

// 8. LRUCache reset removes all entries and resets cache length and itemCount to 0
{
  const cache = new LRU({ max: 5 });
  cache.set("item1", "v1");
  cache.set("item2", "v2");
  assert.equal(cache.itemCount, 2);

  cache.reset();
  assert.equal(cache.itemCount, 0);
  assert.equal(cache.length, 0);
  assert.equal(cache.get("item1"), undefined);
  assert.equal(cache.get("item2"), undefined);
  assert.deepEqual(cache.keys(), []);
}

console.log("All LRUCache@5.1.1 contract tests passed.");

Seeder de origen

anónimo