-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcheck-actorless-executor-operations.test.ts
More file actions
138 lines (117 loc) · 4.49 KB
/
Copy pathcheck-actorless-executor-operations.test.ts
File metadata and controls
138 lines (117 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { describe, expect, it } from 'vitest'
import {
auditSubjectRequirements,
parseOperationPolicies,
referencedOperations,
} from './check-actorless-executor-operations'
describe('operation policy parsing', () => {
it('reads an inline delegatedServices list', () => {
const policies = parseOperationPolicies(`
export const logOperations = {
list: defineWorkspaceOperation({
id: 'logs.list',
delegatedServices: ['copilot', 'executor'],
}),
readStats: defineWorkspaceOperation({
id: 'logs.read_stats',
delegatedServices: ['copilot'],
}),
} as const
`)
expect(policies.get('logOperations.list')).toBe(true)
expect(policies.get('logOperations.readStats')).toBe(false)
})
it('resolves a policy spread into the definition', () => {
const policies = parseOperationPolicies(`
const READER_POLICY = {
principalKinds: ['session', 'delegated'],
delegatedServices: ['copilot', 'executor'],
} as const
export const logOperations = {
readDetail: defineWorkspaceOperation({ id: 'logs.read_detail', ...READER_POLICY }),
} as const
`)
expect(policies.get('logOperations.readDetail')).toBe(true)
})
it('resolves an operation declared through a same-file factory', () => {
const policies = parseOperationPolicies(`
const TOOL_POLICY = { delegatedServices: ['copilot', 'executor'] } as const
const UI_POLICY = { delegatedServices: ['copilot'] } as const
function toolReadOperation<const Id extends string>(id: Id) {
return defineWorkspaceOperation({ id, minimumRole: 'read', ...TOOL_POLICY })
}
function readOperation<const Id extends string>(id: Id) {
return defineWorkspaceOperation({ id, minimumRole: 'read', ...UI_POLICY })
}
export const tableOperations = {
queryRows: toolReadOperation('tables.rows.query'),
listTables: readOperation('tables.list'),
} as const
`)
expect(policies.get('tableOperations.queryRows')).toBe(true)
expect(policies.get('tableOperations.listTables')).toBe(false)
})
it('treats an operation with no delegated services as executor-free', () => {
const policies = parseOperationPolicies(`
export const workspaceOperations = {
read: defineWorkspaceOperation({ id: 'workspaces.read', minimumRole: 'read' }),
} as const
`)
expect(policies.get('workspaceOperations.read')).toBe(false)
})
})
describe('operation references', () => {
it('collects only declared operations', () => {
const referenced = referencedOperations(
`
const useCase = defineAuthorizedWorkspaceUseCase({
operation: logOperations.readDetail,
execute: () => input.signal?.throwIfAborted(),
})
`,
new Set(['logOperations.readDetail', 'logOperations.list'])
)
expect(referenced).toEqual(['logOperations.readDetail'])
})
})
describe('subject requirement audit', () => {
const call = ' const userId = requirePrincipalSubjectUserId(principal)'
it('flags an unannotated call', () => {
const findings = auditSubjectRequirements(`function run() {\n${call}\n}`, ['ops.thing'])
expect(findings).toEqual([
{ file: '', line: 2, reason: 'unannotated', operations: ['ops.thing'] },
])
})
it('accepts an annotated call', () => {
const source = `function run() {\n // actorless-unsupported: skills belong to a person\n${call}\n}`
expect(auditSubjectRequirements(source, [])).toEqual([])
})
it('tolerates context comments above the annotation', () => {
const source = [
'function run() {',
' // The library is per-user.',
' // actorless-unsupported: skills belong to a person',
call,
'}',
].join('\n')
expect(auditSubjectRequirements(source, [])).toEqual([])
})
it('rejects an annotation with no reason', () => {
const source = `function run() {\n // actorless-unsupported:\n${call}\n}`
expect(auditSubjectRequirements(source, [])).toEqual([
{ file: '', line: 3, reason: 'empty-reason', operations: [] },
])
})
it('ignores an annotation separated from the call by code', () => {
const source = [
'function run() {',
' // actorless-unsupported: not attached to the call below',
' const workspaceId = context.workspaceId',
call,
'}',
].join('\n')
expect(auditSubjectRequirements(source, [])).toEqual([
{ file: '', line: 4, reason: 'unannotated', operations: [] },
])
})
})