|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import path from 'node:path'; |
| 3 | +import os from 'node:os'; |
| 4 | +import fse from 'fs-extra'; |
| 5 | + |
| 6 | +import { buildHandlerRegistry } from '../hook-handlers.js'; |
| 7 | +import type { LocalConfig } from '../types.js'; |
| 8 | + |
| 9 | +describe('Cursor gets the user-scope rules from its session-start hook in a workspace outside the home directory (#1197)', () => { |
| 10 | + let tmpDir: string; |
| 11 | + let home: string; |
| 12 | + let userRepo: string; |
| 13 | + let userConfig: LocalConfig; |
| 14 | + |
| 15 | + const context = async (cwd: string, tool = 'cursor', config: LocalConfig | null = userConfig): Promise<string | null> => { |
| 16 | + const registration = buildHandlerRegistry().find((r) => r.event === 'session-start' && r.handler.name === 'cursor-user-rules'); |
| 17 | + const output = await registration!.handler.execute({ hook_event_name: 'SessionStart', cwd }, tool, config); |
| 18 | + if (output === null) return null; |
| 19 | + const parsed = JSON.parse(output) as { hookSpecificOutput: { hookEventName: string; additionalContext: string } }; |
| 20 | + expect(parsed.hookSpecificOutput.hookEventName).toBe('SessionStart'); |
| 21 | + return parsed.hookSpecificOutput.additionalContext; |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + tmpDir = await fse.realpath(await fse.mkdtemp(path.join(os.tmpdir(), 'teamai-cursor-user-rules-'))); |
| 26 | + home = path.join(tmpDir, 'home'); |
| 27 | + vi.stubEnv('HOME', home); |
| 28 | + vi.stubEnv('USERPROFILE', home); |
| 29 | + userRepo = path.join(tmpDir, 'user-team-repo'); |
| 30 | + await fse.outputFile(path.join(userRepo, 'teamai.yaml'), 'team: test\nrepo: https://example.invalid/x/team.git\n'); |
| 31 | + await fse.outputFile(path.join(userRepo, 'rules', 'codeword.md'), 'The team codeword is PELICAN-42.\n'); |
| 32 | + await fse.outputFile(path.join(userRepo, 'rules', 'scoped.md'), '---\npaths:\n - "src/**"\n---\nPrefer named exports.\n'); |
| 33 | + await fse.outputFile( |
| 34 | + path.join(home, '.teamai', 'config.yaml'), |
| 35 | + `repo:\n localPath: ${userRepo}\n remote: https://example.invalid/x/team.git\nusername: u\n`, |
| 36 | + ); |
| 37 | + await fse.ensureDir(path.join(home, '.cursor', 'rules')); |
| 38 | + userConfig = { |
| 39 | + repo: { localPath: userRepo, remote: 'https://example.invalid/x/team.git' }, |
| 40 | + username: 'u', |
| 41 | + additionalRoles: [], |
| 42 | + scope: 'user', |
| 43 | + } as unknown as LocalConfig; |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + vi.unstubAllEnvs(); |
| 48 | + await fse.remove(tmpDir); |
| 49 | + }); |
| 50 | + |
| 51 | + it('adds the user rules when the workspace is outside the home directory, a path-scoped rule after its globs', async () => { |
| 52 | + const workspace = path.join(tmpDir, 'work', 'project'); |
| 53 | + await fse.ensureDir(workspace); |
| 54 | + |
| 55 | + const text = await context(workspace); |
| 56 | + |
| 57 | + expect(text).toContain('The team codeword is PELICAN-42.'); |
| 58 | + expect(text).toContain('Applies to files matching: src/**\nPrefer named exports.'); |
| 59 | + expect(text).not.toContain('paths:'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('adds the user-scope culture and shared instructions, which ~/.cursor/rules also holds', async () => { |
| 63 | + await fse.outputFile(path.join(userRepo, 'culture.md'), '---\ncompany:\n name: Acme\n---\n\nBe kind to teammates.\n'); |
| 64 | + await fse.outputFile(path.join(userRepo, 'claudemd', 'shared.md'), 'Shared team instructions.\n'); |
| 65 | + |
| 66 | + const text = (await context(path.join(tmpDir, 'work')))!; |
| 67 | + |
| 68 | + expect(text).toContain('Be kind to teammates.'); |
| 69 | + expect(text).toContain('Shared team instructions.'); |
| 70 | + expect(text).toContain('PELICAN-42'); |
| 71 | + expect(text).not.toContain('<!--'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('adds nothing under the home directory, where Cursor loads ~/.cursor/rules itself', async () => { |
| 75 | + const workspace = path.join(home, 'src', 'project'); |
| 76 | + await fse.ensureDir(workspace); |
| 77 | + |
| 78 | + expect(await context(workspace)).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('adds nothing when a link on the walk already points at ~/.cursor/rules', async () => { |
| 82 | + const workspace = path.join(tmpDir, 'work', 'project'); |
| 83 | + await fse.ensureDir(path.join(tmpDir, 'work', '.cursor')); |
| 84 | + await fse.symlink(path.join(home, '.cursor', 'rules'), path.join(tmpDir, 'work', '.cursor', 'rules'), 'junction'); |
| 85 | + await fse.ensureDir(workspace); |
| 86 | + |
| 87 | + expect(await context(workspace)).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('adds the user rules, not the project\'s, in a teamai project outside the home directory', async () => { |
| 91 | + const projectRepo = path.join(tmpDir, 'project-team-repo'); |
| 92 | + await fse.outputFile(path.join(projectRepo, 'teamai.yaml'), 'team: proj\nrepo: https://example.invalid/x/proj.git\n'); |
| 93 | + await fse.outputFile(path.join(projectRepo, 'rules', 'project.md'), 'The project codeword is HERON-7.\n'); |
| 94 | + const projectRoot = path.join(tmpDir, 'work', 'project'); |
| 95 | + await fse.ensureDir(projectRoot); |
| 96 | + const projectConfig = { |
| 97 | + repo: { localPath: projectRepo, remote: 'https://example.invalid/x/proj.git' }, |
| 98 | + username: 'u', |
| 99 | + additionalRoles: [], |
| 100 | + scope: 'project', |
| 101 | + projectRoot, |
| 102 | + } as unknown as LocalConfig; |
| 103 | + |
| 104 | + const text = await context(projectRoot, 'cursor', projectConfig); |
| 105 | + |
| 106 | + expect(text).toContain('PELICAN-42'); |
| 107 | + expect(text).not.toContain('HERON-7'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('adds nothing for a tool other than Cursor, or when Cursor is excluded from the user scope', async () => { |
| 111 | + const workspace = path.join(tmpDir, 'work'); |
| 112 | + await fse.ensureDir(workspace); |
| 113 | + |
| 114 | + expect(await context(workspace, 'claude')).toBeNull(); |
| 115 | + expect(await context(workspace, 'cursor', { ...userConfig, disabledAgents: ['cursor'] } as LocalConfig)).toBeNull(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('adds nothing when the text is over Cursor\'s limit, which would drop the whole hook reply', async () => { |
| 119 | + await fse.outputFile(path.join(userRepo, 'rules', 'long.md'), `${'x'.repeat(10_000)}\n`); |
| 120 | + const workspace = path.join(tmpDir, 'work'); |
| 121 | + await fse.ensureDir(workspace); |
| 122 | + |
| 123 | + expect(await context(workspace)).toBeNull(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments