CodeSampleX

Exemple

ansi-styles 6.2.3: bgColor

Échantillon vérifié pour npm ansi-styles 6.2.3: bgColor. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:caf615d8f931f328be77e354231aa3b58ac29b193e5f5a708e291cc4fbb69cf4

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-23
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

Cas

HOW
Objectif
verify ansi-styles.bgColor in pkg:npm/ansi-styles@6.2.3
Paquets
Symboles
  • ansi-styles.bgColor
Environnement
node 22.23.2
Créé
2026-08-23T05:30:29Z

Contrat

  1. ansiStyles.bgColor provides standard and bright background colors with matching open and close ANSI escape sequences
  2. ansiStyles.bgColor.close defines the default background reset escape sequence
  3. ansiStyles.bgColor.ansi converts standard ANSI color codes to background escape sequences
  4. ansiStyles.bgColor.ansi256 generates 256-color palette background ANSI escape codes
  5. ansiStyles.bgColor.ansi16m converts RGB values into 24-bit truecolor background ANSI escape codes
  6. applyBackgroundColor applies background color styling including standard names, 256-color palette, and truecolor RGB to text

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 ansi-styles.bgColor in pkg:npm/ansi-styles@6.2.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-styles@6.2.3
Demonstrate these symbols/APIs:
  - ansi-styles.bgColor
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:22e5b45764d8255b36c5aacd51e0ca8d80c94171bb2e67e2c9b7a53356d25768","constraints":{"executionContext":"node"},"contract":["ansiStyles.bgColor provides standard and bright background colors with matching open and close ANSI escape sequences","ansiStyles.bgColor.close defines the default background reset escape sequence","ansiStyles.bgColor.ansi converts standard ANSI color codes to background escape sequences","ansiStyles.bgColor.ansi256 generates 256-color palette background ANSI escape codes","ansiStyles.bgColor.ansi16m converts RGB values into 24-bit truecolor background ANSI escape codes","applyBackgroundColor applies background color styling including standard names, 256-color palette, and truecolor RGB to text"],"goal":"verify ansi-styles.bgColor in pkg:npm/ansi-styles@6.2.3","kind":"HOW","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"symbols":["ansi-styles.bgColor"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"subject":"pkg:npm/ansi-styles@6.2.3","symbols":["ansi-styles.bgColor"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-styles-bg-color",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-styles-bg-color",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-styles": "6.2.3"
      }
    },
    "node_modules/ansi-styles": {
      "version": "6.2.3",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz",
      "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-styles-bg-color",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for ansi-styles bgColor",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "ansi-styles": "6.2.3"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansi-styles.bgColor in pkg:npm/ansi-styles@6.2.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-styles@6.2.3"
  ],
  "symbols": [
    "ansi-styles.bgColor"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const ansiStylesModule = require('ansi-styles');

const ansiStyles = ansiStylesModule.default || ansiStylesModule;
const { bgColor } = ansiStyles;

/**
 * Returns the bgColor object from ansi-styles.
 * @returns {object}
 */
function getBgColor() {
  return bgColor;
}

/**
 * Retrieves standard or bright background color style pair.
 * @param {string} name
 * @returns {{ open: string, close: string }}
 */
function getNamedBgColor(name) {
  const style = bgColor[name];
  if (!style) {
    throw new Error(`Unknown background color: ${name}`);
  }
  return {
    open: style.open,
    close: style.close,
  };
}

/**
 * Formats a background color escape sequence using a 256-color palette index.
 * @param {number} code
 * @returns {{ open: string, close: string }}
 */
function getAnsi256BgColor(code) {
  if (typeof code !== 'number' || code < 0 || code > 255) {
    throw new RangeError(`Color code must be between 0 and 255: ${code}`);
  }
  return {
    open: bgColor.ansi256(code),
    close: bgColor.close,
  };
}

/**
 * Formats a background color escape sequence using 24-bit truecolor RGB values.
 * @param {number} red
 * @param {number} green
 * @param {number} blue
 * @returns {{ open: string, close: string }}
 */
function getAnsi16mBgColor(red, green, blue) {
  return {
    open: bgColor.ansi16m(red, green, blue),
    close: bgColor.close,
  };
}

/**
 * Applies background styling to the provided text.
 * @param {string} text
 * @param {{ name?: string, ansi256?: number, rgb?: [number, number, number] }} options
 * @returns {string}
 */
function applyBackgroundColor(text, options = {}) {
  let openSequence = '';
  const closeSequence = bgColor.close;

  if (options.name) {
    openSequence = getNamedBgColor(options.name).open;
  } else if (typeof options.ansi256 === 'number') {
    openSequence = getAnsi256BgColor(options.ansi256).open;
  } else if (Array.isArray(options.rgb) && options.rgb.length === 3) {
    const [r, g, b] = options.rgb;
    openSequence = getAnsi16mBgColor(r, g, b).open;
  } else {
    throw new Error('Must specify one of name, ansi256, or rgb options');
  }

  return `${openSequence}${text}${closeSequence}`;
}

module.exports = {
  ansiStyles,
  bgColor,
  getBgColor,
  getNamedBgColor,
  getAnsi256BgColor,
  getAnsi16mBgColor,
  applyBackgroundColor,
};
test/contract.js
const assert = require('node:assert');
const {
  ansiStyles,
  bgColor,
  getBgColor,
  getNamedBgColor,
  getAnsi256BgColor,
  getAnsi16mBgColor,
  applyBackgroundColor,
} = require('../src/index.js');

// 1. ansiStyles.bgColor provides standard and bright background colors with matching open and close ANSI escape sequences
assert(bgColor && typeof bgColor === 'object', 'bgColor must be an object');
assert.strictEqual(getBgColor(), bgColor);

const standardBgColors = {
  bgBlack: '\u001B[40m',
  bgRed: '\u001B[41m',
  bgGreen: '\u001B[42m',
  bgYellow: '\u001B[43m',
  bgBlue: '\u001B[44m',
  bgMagenta: '\u001B[45m',
  bgCyan: '\u001B[46m',
  bgWhite: '\u001B[47m',
};

for (const [name, expectedOpen] of Object.entries(standardBgColors)) {
  const style = bgColor[name];
  assert(style, `Missing standard style ${name}`);
  assert.strictEqual(style.open, expectedOpen, `${name} open code mismatch`);
  assert.strictEqual(style.close, '\u001B[49m', `${name} close code mismatch`);

  const named = getNamedBgColor(name);
  assert.strictEqual(named.open, expectedOpen);
  assert.strictEqual(named.close, '\u001B[49m');
}

const brightBgColors = {
  bgBlackBright: '\u001B[100m',
  bgRedBright: '\u001B[101m',
  bgGreenBright: '\u001B[102m',
  bgYellowBright: '\u001B[103m',
  bgBlueBright: '\u001B[104m',
  bgMagentaBright: '\u001B[105m',
  bgCyanBright: '\u001B[106m',
  bgWhiteBright: '\u001B[107m',
};

for (const [name, expectedOpen] of Object.entries(brightBgColors)) {
  const style = bgColor[name];
  assert(style, `Missing bright style ${name}`);
  assert.strictEqual(style.open, expectedOpen, `${name} open code mismatch`);
  assert.strictEqual(style.close, '\u001B[49m', `${name} close code mismatch`);
}

// Aliases
assert.deepStrictEqual(bgColor.bgGray, bgColor.bgBlackBright);
assert.deepStrictEqual(bgColor.bgGrey, bgColor.bgBlackBright);

// 2. ansiStyles.bgColor.close defines the default background reset escape sequence
assert.strictEqual(bgColor.close, '\u001B[49m');

// 3. ansiStyles.bgColor.ansi converts standard ANSI color codes to background escape sequences
assert(typeof bgColor.ansi === 'function', 'bgColor.ansi must be a function');
assert.strictEqual(bgColor.ansi(30), '\u001B[40m');
assert.strictEqual(bgColor.ansi(31), '\u001B[41m');
assert.strictEqual(bgColor.ansi(32), '\u001B[42m');
assert.strictEqual(bgColor.ansi(37), '\u001B[47m');

// 4. ansiStyles.bgColor.ansi256 generates 256-color palette background ANSI escape codes
assert(typeof bgColor.ansi256 === 'function', 'bgColor.ansi256 must be a function');
assert.strictEqual(bgColor.ansi256(0), '\u001B[48;5;0m');
assert.strictEqual(bgColor.ansi256(100), '\u001B[48;5;100m');
assert.strictEqual(bgColor.ansi256(255), '\u001B[48;5;255m');

const bg256 = getAnsi256BgColor(200);
assert.strictEqual(bg256.open, '\u001B[48;5;200m');
assert.strictEqual(bg256.close, '\u001B[49m');

assert.throws(() => getAnsi256BgColor(-1), RangeError);
assert.throws(() => getAnsi256BgColor(256), RangeError);

// 5. ansiStyles.bgColor.ansi16m converts RGB values into 24-bit truecolor background ANSI escape codes
assert(typeof bgColor.ansi16m === 'function', 'bgColor.ansi16m must be a function');
assert.strictEqual(bgColor.ansi16m(255, 128, 0), '\u001B[48;2;255;128;0m');
assert.strictEqual(bgColor.ansi16m(0, 0, 0), '\u001B[48;2;0;0;0m');
assert.strictEqual(bgColor.ansi16m(12, 34, 56), '\u001B[48;2;12;34;56m');

const bg16m = getAnsi16mBgColor(10, 20, 30);
assert.strictEqual(bg16m.open, '\u001B[48;2;10;20;30m');
assert.strictEqual(bg16m.close, '\u001B[49m');

// 6. applyBackgroundColor applies background color styling including standard names, 256-color palette, and truecolor RGB to text
const message = 'Sample Text';
assert.strictEqual(applyBackgroundColor(message, { name: 'bgCyan' }), `\u001B[46m${message}\u001B[49m`);
assert.strictEqual(applyBackgroundColor(message, { name: 'bgRedBright' }), `\u001B[101m${message}\u001B[49m`);
assert.strictEqual(applyBackgroundColor(message, { ansi256: 45 }), `\u001B[48;5;45m${message}\u001B[49m`);
assert.strictEqual(applyBackgroundColor(message, { rgb: [255, 100, 50] }), `\u001B[48;2;255;100;50m${message}\u001B[49m`);

assert.throws(() => applyBackgroundColor(message, { name: 'invalidBg' }), /Unknown background color: invalidBg/);
assert.throws(() => applyBackgroundColor(message, {}), /Must specify one of name, ansi256, or rgb options/);

console.log('All contract assertions passed successfully.');

Seeder d'origine

anonyme