Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
TypeScript
telemetry, agents-md: the test plugins hook and call the collector stream by name, and stand in for telemetry through its events (#96930)
Alice T'Poteate1bb7b065bc2
diff --git a/mods/agents-md/tests/fixtures/recording.ts b/mods/agents-md/tests/fixtures/recording.tsindex b0a8cdbc..84656931 100644--- a/mods/agents-md/tests/fixtures/recording.ts+++ b/mods/agents-md/tests/fixtures/recording.ts@@ -1,29 +1,38 @@import type { Plugin } from 'claude-code/testing'/**- * A plugin standing in for the telemetry built-in: it adds the `telemetry`- * noun at `engine.create` over the nouns beneath.+ * A plugin standing in for the telemetry built-in: it hooks `telemetry.log`+ * and `telemetry.mark` and answers them without going on.*- * Its `log` and `mark` write the entry they are handed as one `$.ui.log`- * line, `<op> <json>`, which startedOf keeps and rowsOf reads back.+ * Each writes the entry as the caller handed it (`to` left off) as one+ * `$.ui.log` line, `<op> <json>`, which startedOf keeps and rowsOf reads+ * back. Where the engine has no `telemetry` its engine.create step adds one.*/export const RECORDING: Plugin = {name: 'recording',tier: 'builtin',register(on) {+ on('telemetry.log', async ($, e) => {+ const { to: _to, ...entry } = e++ await $.ui.log(`log ${JSON.stringify(entry)}`)++ return { value: undefined }+ })++ on('telemetry.mark', async ($, e) => {+ await $.ui.log(`mark ${JSON.stringify(e)}`)++ return { value: undefined }+ })+on('engine.create', async ($, e, next) => {const beneath = await next(e)- const built = { ...beneath }- built.telemetry = {- log: async entry => {- await beneath.ui.log(`log ${JSON.stringify(entry)}`)- },- mark: async entry => {- await beneath.ui.log(`mark ${JSON.stringify(entry)}`)- },+ const added = {+ telemetry: { log: async () => undefined, mark: async () => undefined },}- return built+ return { ...added, ...beneath }})},}diff --git a/mods/telemetry/tests/fixtures/plugins/holding.ts b/mods/telemetry/tests/fixtures/plugins/holding.tsindex e7017aad..eec383f0 100644--- a/mods/telemetry/tests/fixtures/plugins/holding.ts+++ b/mods/telemetry/tests/fixtures/plugins/holding.ts@@ -21,7 +21,7 @@ export const holding: Plugin = {})on('command.run', { command: 'hold' }, async () => {- await held?.telemetry.log({ event: 'held' })+ await held?.telemetry.log({ to: 'collector', event: 'held' })return { text: 'served' }})diff --git a/mods/telemetry/tests/fixtures/plugins/meddling.ts b/mods/telemetry/tests/fixtures/plugins/meddling.tsindex 8c19d26e..91548b4a 100644--- a/mods/telemetry/tests/fixtures/plugins/meddling.ts+++ b/mods/telemetry/tests/fixtures/plugins/meddling.ts@@ -1,17 +1,20 @@import type { Plugin } from 'claude-code/testing'/**- * A plugin a person installed that hooks `telemetry.log` above the gate to- * rename every row, and logs one of its own through `/meddle`.+ * A plugin a person installed that hooks `telemetry.log` above the gate for+ * the one stream it may, the collector's, to rename every record there, and+ * logs one of its own through `/meddle`.*/export const meddling: Plugin = {name: 'meddling',tier: 'user',register(on) {- on('telemetry.log', (_$, e, next) => next({ ...e, event: 'renamed' }))+ on('telemetry.log', { to: 'collector' }, (_$, e, next) =>+ next({ ...e, event: 'renamed' }),+ )on('command.run', { command: 'meddle' }, $ =>- $.telemetry.log({ event: 'mine' }).then(+ $.telemetry.log({ to: 'collector', event: 'mine' }).then(() => ({ text: 'served' }),(error: unknown) => ({ text: String(error) }),),diff --git a/mods/telemetry/tests/fixtures/plugins/reaching.ts b/mods/telemetry/tests/fixtures/plugins/reaching.tsindex 542900db..4386c297 100644--- a/mods/telemetry/tests/fixtures/plugins/reaching.ts+++ b/mods/telemetry/tests/fixtures/plugins/reaching.ts@@ -17,7 +17,7 @@ export const reaching: Plugin = {const beneath = await next(e)during = await beneath.telemetry- .log({ event: 'during_create' })+ .log({ to: 'collector', event: 'during_create' }).then(() => 'served', String)return beneath@@ -26,7 +26,7 @@ export const reaching: Plugin = {on('session.start', ($, e, next) => {$.clock.after(1, () => {void $.telemetry- .log({ event: 'from_timer' })+ .log({ to: 'collector', event: 'from_timer' }).then(() => 'served', String).then(outcome => $.ui.log(`timer: ${outcome}`, { to: 'debug' }))})@@ -36,7 +36,7 @@ export const reaching: Plugin = {on('tool.call', { tool: 'Read' }, async ($, e, next) => {const outcome = await $.telemetry- .log({ event: 'from_tool' })+ .log({ to: 'collector', event: 'from_tool' }).then(() => 'served', String)await $.ui.log(`tool: ${outcome}`, { to: 'debug' })diff --git a/mods/telemetry/tests/fixtures/plugins/replacing.ts b/mods/telemetry/tests/fixtures/plugins/replacing.tsindex d52d5941..e6246c7c 100644--- a/mods/telemetry/tests/fixtures/plugins/replacing.ts+++ b/mods/telemetry/tests/fixtures/plugins/replacing.ts@@ -16,7 +16,7 @@ export const replacing: Plugin = {}))on('command.run', { command: 'replace' }, $ =>- $.telemetry.log({ event: 'mine' }).then(+ $.telemetry.log({ to: 'collector', event: 'mine' }).then(() => ({ text: 'served' }),(error: unknown) => ({ text: String(error) }),),diff --git a/mods/telemetry/tests/fixtures/plugins/swallowing.ts b/mods/telemetry/tests/fixtures/plugins/swallowing.tsindex af9be34c..1797f92b 100644--- a/mods/telemetry/tests/fixtures/plugins/swallowing.ts+++ b/mods/telemetry/tests/fixtures/plugins/swallowing.ts@@ -2,12 +2,13 @@ import type { Plugin } from 'claude-code/testing'/*** A plugin a person installed that answers `telemetry.log` above the gate- * without going on beneath: the rows of everyone beneath it stop there.+ * without going on beneath, for the one stream it may hook, the+ * collector's: the records of everyone beneath it stop there.*/export const swallowing: Plugin = {name: 'swallowing',tier: 'user',register(on) {- on('telemetry.log', () => ({ value: undefined }))+ on('telemetry.log', { to: 'collector' }, () => ({ value: undefined }))},}diff --git a/mods/telemetry/tests/fixtures/plugins/visiting.ts b/mods/telemetry/tests/fixtures/plugins/visiting.tsindex 7ce60d89..8a004385 100644--- a/mods/telemetry/tests/fixtures/plugins/visiting.ts+++ b/mods/telemetry/tests/fixtures/plugins/visiting.ts@@ -1,18 +1,21 @@import type { Plugin } from 'claude-code/testing'/**- * A plugin a person installed whose `/visit <entry>` logs the entry through- * `$.telemetry`, answering "queued", or why the row was refused.+ * A plugin a person installed whose `/visit <entry>` logs the entry's event+ * and properties through `$.telemetry`, naming the one stream it may, the+ * collector's; it answers "queued", or why the call was refused.*/export const visiting: Plugin = {name: 'visiting',tier: 'user',register(on) {- on('command.run', { command: 'visit' }, ($, e) =>- $.telemetry.log(JSON.parse(e.args)).then(+ on('command.run', { command: 'visit' }, ($, e) => {+ const { event, props } = JSON.parse(e.args)++ return $.telemetry.log({ to: 'collector', event, props }).then(() => ({ text: 'queued' }),(error: unknown) => ({ text: String(error) }),- ),- )+ )+ })},}diff --git a/mods/telemetry/tests/gate.test.ts b/mods/telemetry/tests/gate.test.tsindex 45e479a9..15f412fd 100644--- a/mods/telemetry/tests/gate.test.ts+++ b/mods/telemetry/tests/gate.test.ts@@ -61,7 +61,7 @@ describe('gate', () => {)test(- 'a hook above may rename a row a built-in sends, never send its own',+ "a hook above on the collector's stream renames no row, sends none",{ plugins: [Fixtures.recording, Fixtures.meddling] },async ($, on) => {mock.env(on, Fixtures.SENDING_ENV)@@ -77,13 +77,13 @@ describe('gate', () => {rows: Fixtures.rowsOf(session).map(Fixtures.eventNameOf),}).toEqual({own: `HooksError: meddling: $.telemetry.log: ${Hooks.REFUSED.deny}`,- rows: ['tengu_plugin_renamed'],+ rows: ['tengu_plugin_survey_answered'],})},)test(- 'a hook above that answers without going on sends nothing at all',+ "a hook above that swallows the collector's stream stops no row",{ plugins: [Fixtures.recording, Fixtures.swallowing] },async ($, on) => {mock.env(on, Fixtures.SENDING_ENV)@@ -96,9 +96,14 @@ describe('gate', () => {await session.clock.advance(Hooks.BATCH_WINDOW_MS)- expect({ answer, posts: session.posts }).toEqual({+ expect({+ answer,+ posts: session.posts.length,+ rows: Fixtures.rowsOf(session).map(Fixtures.eventNameOf),+ }).toEqual({answer: 'queued',- posts: [],+ posts: 1,+ rows: ['tengu_plugin_survey_answered'],})},)