spacehub

surshar / claude-code

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.

Built by Anthropic · github.com/anthropics

claude-code / mods / sec-default / tests / policy / create-policy-memo.test.ts
72 lines · 1.7 KBRaw
1import type { Settings } from 'claude-code'
2import { describe, expect, test, tier } from 'claude-code/testing'
3
4import Policy from '../../hooks/policy'
5import Fixtures from '../fixtures'
6
7tier('prepend')
8
9describe('create-policy-memo', () => {
10 function countedRead() {
11 let reads = 0
12
13 function read(): Promise<Settings> {
14 reads += 1
15
16 return Promise.resolve(Fixtures.MANAGED_POLICY)
17 }
18
19 return { read, reads: () => reads }
20 }
21
22 test('a burst of reads inside the window is one read', async () => {
23 let at = 0
24
25 const memo = Policy.createPolicyMemo(Policy.POLICY_MEMO_MS, () => at)
26 const { read, reads } = countedRead()
27 const burst = await Promise.all([memo(read), memo(read), memo(read)])
28
29 at += Policy.POLICY_MEMO_MS
30
31 await memo(read)
32
33 expect(burst).toEqual([
34 Fixtures.MANAGED_POLICY,
35 Fixtures.MANAGED_POLICY,
36 Fixtures.MANAGED_POLICY,
37 ])
38
39 expect(reads()).toBe(1)
40 })
41
42 test('past the window the next decision reads again', async () => {
43 let at = 0
44
45 const memo = Policy.createPolicyMemo(Policy.POLICY_MEMO_MS, () => at)
46 const { read, reads } = countedRead()
47
48 await memo(read)
49
50 at += Policy.POLICY_MEMO_MS + 1
51
52 await memo(read)
53
54 expect(reads()).toBe(2)
55 })
56
57 test('a failed read is served for its window: fails closed', async () => {
58 const memo = Policy.createPolicyMemo(Policy.POLICY_MEMO_MS, () => 0)
59
60 let reads = 0
61
62 function failing(): Promise<Settings> {
63 reads += 1
64
65 return Promise.reject(new Error('settings unreadable'))
66 }
67
68 await expect(memo(failing)).rejects.toThrow('settings unreadable')
69 await expect(memo(failing)).rejects.toThrow('settings unreadable')
70 expect(reads).toBe(1)
71 })
72})