import { describe, it, expect } from 'vitest' import { transformCan } from '../src/runtime/transformer/transform-can' const TEST_FILE = `${process.cwd()}/components/Test.vue` const buildSFC = (template: string) => `\n${template}\n` const runTransform = (template: string) => { const result = transformCan({ code: buildSFC(template), id: TEST_FILE }) return result?.code ?? '' } describe('transformCan', () => { it('injects __can__ guards and a matching v-cannot block', () => { const code = runTransform(`
Refus
`) expect(code).toContain(`v-if="__can__('employee', 'view')"`) expect(code).toContain(`v-if="!(__can__('employee', 'view'))"`) }) it('merges existing v-if expressions with the generated guard', () => { const code = runTransform(` `) expect(code).toContain(`v-if="__can__('contract', 'create') && (isReady)"`) }) it('throws when v-cannot is used without a preceding v-can', () => { const exec = () => transformCan({ code: buildSFC('Denied
'), id: TEST_FILE, }) expect(exec).toThrow(/without an expression must immediately follow/) }) it('throws when the expression does not start with can.*', () => { const exec = () => transformCan({ code: buildSFC(''), id: TEST_FILE, }) expect(exec).toThrow(/expressions must start with `can\.`/) }) it('mirrors guards across v-if / v-else-if / v-else chains when the first branch uses v-can', () => { const code = runTransform(`Denied
`) expect(code).toContain(`v-if="__can__('employee', 'view') && (ready)"`) expect(code).toContain(`v-else-if="__can__('employee', 'view') && (later)"`) expect(code).toContain(`v-else-if="__can__('employee', 'view')"`) expect(code).toContain(`v-if="!(__can__('employee', 'view'))"`) }) it('throws when branches declare different v-can expressions', () => { const exec = () => transformCan({ code: buildSFC(` `), id: TEST_FILE, }) expect(exec).toThrow(/must match across every branch/) }) it('allows explicit expressions on v-cannot without a preceding v-can', () => { const code = runTransform('Denied
') expect(code).toContain(`v-if="!(__can__('employee', 'view'))"`) }) it('requires adjacent placement for v-cannot without an expression', () => { const exec = () => transformCan({ code: buildSFC(`Denied