CodeSampleX

Beispiel

@sinonjs/fake-timers 10.3.0: createClock

Verifiziertes Beispiel für npm @sinonjs/fake-timers 10.3.0: createClock. Der Vertrag lief auf node 22 · linux debian/x64 · docker und bestand.

sha256:004b12092a8414584ea50a99509986c451d4fceab5c3ad4822ffa53098902e60

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-04

Fall

HOW
Ziel
verify @sinonjs/fake-timers.createClock in pkg:npm/%40sinonjs/fake-timers@10.3.0
Pakete
Symbole
  • @sinonjs/fake-timers.createClock
Umgebung
node 22.23.2
Erstellt
2026-09-04T13:15:50Z

Contract

  1. createClock creates an independent fake clock initialized at the specified start timestamp
  2. clock.setTimeout schedules a timer that fires callback with provided arguments when advanced past delay
  3. clock.clearTimeout cancels a scheduled timer so it does not fire
  4. clock.setInterval repeatedly invokes callback every interval until clearInterval
  5. clock.tick synchronously advances clock time by ms and executes expired timers in chronological order
  6. clock.tickAsync advances clock time asynchronously, awaiting resolved promises returned by async callbacks
  7. clock.runAll exhausts and triggers all scheduled timers until the queue is empty
  8. clock.runToLast advances time to the expiration timestamp of the latest scheduled timer and fires pending timers
  9. clock.setSystemTime modifies current clock time without executing scheduled timers
  10. clock.reset resets clock.now back to the initial start timestamp and clears all active timers
  11. clock.countTimers returns the number of active scheduled timers remaining in the queue
  12. clock.next advances clock time to the very next scheduled timer and executes it

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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 @sinonjs/fake-timers.createClock in pkg:npm/%40sinonjs/fake-timers@10.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40sinonjs/fake-timers@10.3.0
Demonstrate these symbols/APIs:
  - @sinonjs/fake-timers.createClock

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:54de9032aa7d2d804bb8141686e4be1adf7aff3038eb6c1671ebe4f909356ccd","contract":["createClock creates an independent fake clock initialized at the specified start timestamp","clock.setTimeout schedules a timer that fires callback with provided arguments when advanced past delay","clock.clearTimeout cancels a scheduled timer so it does not fire","clock.setInterval repeatedly invokes callback every interval until clearInterval","clock.tick synchronously advances clock time by ms and executes expired timers in chronological order","clock.tickAsync advances clock time asynchronously, awaiting resolved promises returned by async callbacks","clock.runAll exhausts and triggers all scheduled timers until the queue is empty","clock.runToLast advances time to the expiration timestamp of the latest scheduled timer and fires pending timers","clock.setSystemTime modifies current clock time without executing scheduled timers","clock.reset resets clock.now back to the initial start timestamp and clears all active timers","clock.countTimers returns the number of active scheduled timers remaining in the queue","clock.next advances clock time to the very next scheduled timer and executes it"],"goal":"verify @sinonjs/fake-timers.createClock in pkg:npm/%40sinonjs/fake-timers@10.3.0","kind":"HOW","packages":["pkg:npm/%40sinonjs/fake-timers@10.3.0"],"schemaVersion":1,"symbols":["@sinonjs/fake-timers.createClock"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40sinonjs/fake-timers@10.3.0"],"schemaVersion":1,"subject":"pkg:npm/%40sinonjs/fake-timers@10.3.0","symbols":["@sinonjs/fake-timers.createClock"],"verifierAdapter":"node-typescript@1"}
index.mjs
import FakeTimers from '@sinonjs/fake-timers';

export const { createClock } = FakeTimers;

/**
 * Creates a standalone Clock instance.
 *
 * @param {number|Date} [start=0] - Initial timestamp for the clock
 * @param {number} [loopLimit=10000] - Max iterations for runAll / runToLast to prevent infinite loops
 * @returns {import('@sinonjs/fake-timers').Clock} Clock instance
 */
export function initializeClock(start = 0, loopLimit = 10000) {
  return createClock(start, loopLimit);
}

export default {
  createClock,
  initializeClock,
};
package-lock.json
{
  "name": "sample-fake-timers-create-clock",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-fake-timers-create-clock",
      "version": "1.0.0",
      "dependencies": {
        "@sinonjs/fake-timers": "10.3.0"
      }
    },
    "node_modules/@sinonjs/commons": {
      "version": "3.0.1",
      "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz",
      "integrity": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "type-detect": "4.0.8"
      }
    },
    "node_modules/@sinonjs/fake-timers": {
      "version": "10.3.0",
      "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-10.3.0.tgz",
      "integrity": "sha512-V4BG07kuYSUkTCSBHG8G8TNhM+F19jXFWnQtzj+we8DrkpSBCee9Z3Ms8yiGer/dlmhe35/Xdgyo3/0rQKg7YA==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "@sinonjs/commons": "^3.0.0"
      }
    },
    "node_modules/type-detect": {
      "version": "4.0.8",
      "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
      "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
      "license": "MIT",
      "engines": {
        "node": ">=4"
      }
    }
  }
}
package.json
{
  "name": "sample-fake-timers-create-clock",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "@sinonjs/fake-timers": "10.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @sinonjs/fake-timers.createClock in pkg:npm/%40sinonjs/fake-timers@10.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40sinonjs/fake-timers@10.3.0"
  ],
  "symbols": [
    "@sinonjs/fake-timers.createClock"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import FakeTimers, { createClock, initializeClock } from '../index.mjs';

// Contract 1: createClock creates an independent fake clock initialized at the specified start timestamp
{
  const initialTime = 5000;
  const clock = initializeClock(initialTime);
  assert.strictEqual(typeof createClock, 'function');
  assert.strictEqual(typeof clock.setTimeout, 'function');
  assert.strictEqual(typeof clock.clearTimeout, 'function');
  assert.strictEqual(typeof clock.setInterval, 'function');
  assert.strictEqual(typeof clock.clearInterval, 'function');
  assert.strictEqual(typeof clock.tick, 'function');
  assert.strictEqual(typeof clock.tickAsync, 'function');
  assert.strictEqual(typeof clock.runAll, 'function');
  assert.strictEqual(typeof clock.runToLast, 'function');
  assert.strictEqual(typeof clock.setSystemTime, 'function');
  assert.strictEqual(typeof clock.reset, 'function');
  assert.strictEqual(typeof clock.countTimers, 'function');
  assert.strictEqual(typeof clock.next, 'function');
  assert.strictEqual(clock.now, initialTime);
}

// Contract 2: clock.setTimeout schedules a timer that fires callback with provided arguments when advanced past delay
{
  const clock = createClock(1000);
  let executed = false;
  let receivedArgs = [];
  clock.setTimeout((arg1, arg2) => {
    executed = true;
    receivedArgs = [arg1, arg2];
  }, 150, 'alpha', 42);

  assert.strictEqual(executed, false);
  clock.tick(149);
  assert.strictEqual(executed, false);
  assert.strictEqual(clock.now, 1149);

  clock.tick(1);
  assert.strictEqual(executed, true);
  assert.deepEqual(receivedArgs, ['alpha', 42]);
  assert.strictEqual(clock.now, 1150);
}

// Contract 3: clock.clearTimeout cancels a scheduled timer so it does not fire
{
  const clock = createClock(0);
  let executed = false;
  const timerId = clock.setTimeout(() => {
    executed = true;
  }, 100);

  clock.clearTimeout(timerId);
  clock.tick(200);
  assert.strictEqual(executed, false);
  assert.strictEqual(clock.countTimers(), 0);
}

// Contract 4: clock.setInterval repeatedly invokes callback every interval until clearInterval
{
  const clock = createClock(0);
  let count = 0;
  const intervalId = clock.setInterval(() => {
    count++;
  }, 50);

  clock.tick(175);
  assert.strictEqual(count, 3);

  clock.clearInterval(intervalId);
  clock.tick(150);
  assert.strictEqual(count, 3);
}

// Contract 5: clock.tick synchronously advances clock time by ms and executes expired timers in chronological order
{
  const clock = createClock(0);
  const log = [];
  clock.setTimeout(() => log.push('timer300'), 300);
  clock.setTimeout(() => log.push('timer100'), 100);
  clock.setTimeout(() => log.push('timer200'), 200);

  clock.tick(250);
  assert.deepEqual(log, ['timer100', 'timer200']);
  assert.strictEqual(clock.now, 250);

  clock.tick(100);
  assert.deepEqual(log, ['timer100', 'timer200', 'timer300']);
  assert.strictEqual(clock.now, 350);
}

// Contract 6: clock.tickAsync advances clock time asynchronously, awaiting resolved promises returned by async callbacks
{
  const clock = createClock(0);
  let asyncFinished = false;
  clock.setTimeout(async () => {
    await Promise.resolve();
    asyncFinished = true;
  }, 100);

  assert.strictEqual(asyncFinished, false);
  await clock.tickAsync(100);
  assert.strictEqual(asyncFinished, true);
}

// Contract 7: clock.runAll exhausts and triggers all scheduled timers until the queue is empty
{
  const clock = createClock(0);
  const order = [];
  clock.setTimeout(() => order.push('first'), 50);
  clock.setTimeout(() => order.push('third'), 150);
  clock.setTimeout(() => order.push('second'), 100);

  assert.strictEqual(clock.countTimers(), 3);
  clock.runAll();
  assert.deepEqual(order, ['first', 'second', 'third']);
  assert.strictEqual(clock.countTimers(), 0);
  assert.strictEqual(clock.now, 150);
}

// Contract 8: clock.runToLast advances time to the expiration timestamp of the latest scheduled timer and fires pending timers
{
  const clock = createClock(0);
  const fired = [];
  clock.setTimeout(() => fired.push('a'), 100);
  clock.setTimeout(() => fired.push('c'), 500);
  clock.setTimeout(() => fired.push('b'), 200);

  clock.runToLast();
  assert.deepEqual(fired, ['a', 'b', 'c']);
  assert.strictEqual(clock.now, 500);
}

// Contract 9: clock.setSystemTime modifies current clock time without executing scheduled timers
{
  const clock = createClock(1000);
  let executed = false;
  clock.setTimeout(() => {
    executed = true;
  }, 500);

  clock.setSystemTime(5000);
  assert.strictEqual(clock.now, 5000);
  assert.strictEqual(executed, false);
  assert.strictEqual(clock.countTimers(), 1);
}

// Contract 10: clock.reset resets clock.now back to the initial start timestamp and clears all active timers
{
  const clock = createClock(2000);
  clock.setTimeout(() => {}, 300);
  clock.setInterval(() => {}, 100);
  clock.tick(500);

  assert.strictEqual(clock.now, 2500);
  assert.strictEqual(clock.countTimers(), 1);

  clock.reset();
  assert.strictEqual(clock.now, 2000);
  assert.strictEqual(clock.countTimers(), 0);
}

// Contract 11: clock.countTimers returns the number of active scheduled timers remaining in the queue
{
  const clock = createClock(0);
  assert.strictEqual(clock.countTimers(), 0);

  const t1 = clock.setTimeout(() => {}, 100);
  const t2 = clock.setTimeout(() => {}, 200);
  const t3 = clock.setInterval(() => {}, 50);
  assert.strictEqual(clock.countTimers(), 3);

  clock.clearTimeout(t1);
  assert.strictEqual(clock.countTimers(), 2);

  clock.clearInterval(t3);
  assert.strictEqual(clock.countTimers(), 1);

  clock.clearTimeout(t2);
  assert.strictEqual(clock.countTimers(), 0);
}

// Contract 12: clock.next advances clock time to the very next scheduled timer and executes it
{
  const clock = createClock(0);
  const log = [];
  clock.setTimeout(() => log.push('first'), 100);
  clock.setTimeout(() => log.push('second'), 250);

  clock.next();
  assert.deepEqual(log, ['first']);
  assert.strictEqual(clock.now, 100);
  assert.strictEqual(clock.countTimers(), 1);

  clock.next();
  assert.deepEqual(log, ['first', 'second']);
  assert.strictEqual(clock.now, 250);
  assert.strictEqual(clock.countTimers(), 0);
}

console.log('All contracts passed.');

Ursprungs-Seeder

anonym