|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { CONTEXT } from './config' |
| 3 | +import { Context } from './context' |
| 4 | +import { defaultOptions } from './engine' |
| 5 | + |
| 6 | +describe('context', () => { |
| 7 | + it('should initialize with default context', () => { |
| 8 | + const context = new Context(defaultOptions) |
| 9 | + expect(context.context).toBe(CONTEXT) |
| 10 | + }) |
| 11 | + |
| 12 | + describe('affix', () => { |
| 13 | + it('should affix string to context', () => { |
| 14 | + const context = new Context(defaultOptions) |
| 15 | + const result = context.affix('test') |
| 16 | + expect(result).toBe('c_test') |
| 17 | + expect(context.context).toBe('c_test') |
| 18 | + }) |
| 19 | + |
| 20 | + it('should affix number to context', () => { |
| 21 | + const context = new Context(defaultOptions) |
| 22 | + const result = context.affix(123) |
| 23 | + expect(result).toBe('c_123') |
| 24 | + expect(context.context).toBe('c_123') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should support multiple affixes', () => { |
| 28 | + const context = new Context(defaultOptions) |
| 29 | + |
| 30 | + context.affix('first') |
| 31 | + expect(context.context).toBe('c_first') |
| 32 | + |
| 33 | + context.affix('second') |
| 34 | + expect(context.context).toBe('c_first_second') |
| 35 | + |
| 36 | + context.affix(42) |
| 37 | + expect(context.context).toBe('c_first_second_42') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should return the new context value', () => { |
| 41 | + const context = new Context(defaultOptions) |
| 42 | + const result1 = context.affix('alpha') |
| 43 | + const result2 = context.affix('beta') |
| 44 | + |
| 45 | + expect(result1).toBe('c_alpha') |
| 46 | + expect(result2).toBe('c_alpha_beta') |
| 47 | + expect(context.context).toBe('c_alpha_beta') |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('reset', () => { |
| 52 | + it('should reset to previous context after single affix', () => { |
| 53 | + const context = new Context(defaultOptions) |
| 54 | + |
| 55 | + context.affix('temp') |
| 56 | + expect(context.context).toBe('c_temp') |
| 57 | + |
| 58 | + context.reset() |
| 59 | + expect(context.context).toBe('c') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should reset to previous context after multiple affixes', () => { |
| 63 | + const context = new Context(defaultOptions) |
| 64 | + |
| 65 | + context.affix('first') |
| 66 | + context.affix('second') |
| 67 | + context.affix('third') |
| 68 | + expect(context.context).toBe('c_first_second_third') |
| 69 | + |
| 70 | + context.reset() |
| 71 | + expect(context.context).toBe('c_first_second') |
| 72 | + |
| 73 | + context.reset() |
| 74 | + expect(context.context).toBe('c_first') |
| 75 | + |
| 76 | + context.reset() |
| 77 | + expect(context.context).toBe('c') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should handle reset on base context gracefully', () => { |
| 81 | + const context = new Context(defaultOptions) |
| 82 | + expect(context.context).toBe('c') |
| 83 | + |
| 84 | + context.reset() |
| 85 | + expect(context.context).toBe('c') |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('nested operations', () => { |
| 90 | + it('should handle complex affix and reset patterns', () => { |
| 91 | + const context = new Context(defaultOptions) |
| 92 | + |
| 93 | + // Build nested context |
| 94 | + context.affix('1') |
| 95 | + expect(context.context).toBe('c_1') |
| 96 | + |
| 97 | + context.affix('2') |
| 98 | + expect(context.context).toBe('c_1_2') |
| 99 | + |
| 100 | + // Reset one level |
| 101 | + context.reset() |
| 102 | + expect(context.context).toBe('c_1') |
| 103 | + |
| 104 | + // Reset one level |
| 105 | + context.reset() |
| 106 | + expect(context.context).toBe('c') |
| 107 | + |
| 108 | + // Keep root level |
| 109 | + context.reset() |
| 110 | + expect(context.context).toBe('c') |
| 111 | + }) |
| 112 | + |
| 113 | + it('should maintain context stack integrity', () => { |
| 114 | + const context = new Context(defaultOptions) |
| 115 | + |
| 116 | + // Create multiple contexts |
| 117 | + const contexts = ['a', 'b', 'c', 'd', 'e'] |
| 118 | + contexts.forEach(ctx => context.affix(ctx)) |
| 119 | + |
| 120 | + expect(context.context).toBe('c_a_b_c_d_e') |
| 121 | + |
| 122 | + // Reset them in reverse order |
| 123 | + for (let i = contexts.length - 1; i >= 0; i--) { |
| 124 | + context.reset() |
| 125 | + const expected = i === 0 ? 'c' : `c_${contexts.slice(0, i).join('_')}` |
| 126 | + expect(context.context).toBe(expected) |
| 127 | + } |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe('edge cases', () => { |
| 132 | + it('should handle empty string affix', () => { |
| 133 | + const context = new Context(defaultOptions) |
| 134 | + const result = context.affix('') |
| 135 | + expect(result).toBe('c_') |
| 136 | + expect(context.context).toBe('c_') |
| 137 | + }) |
| 138 | + |
| 139 | + it('should handle zero as affix', () => { |
| 140 | + const context = new Context(defaultOptions) |
| 141 | + const result = context.affix(0) |
| 142 | + expect(result).toBe('c_0') |
| 143 | + expect(context.context).toBe('c_0') |
| 144 | + }) |
| 145 | + |
| 146 | + it('should handle special characters in affix', () => { |
| 147 | + const context = new Context(defaultOptions) |
| 148 | + const result = context.affix('test-name.property[0]') |
| 149 | + expect(result).toBe('c_test-name.property[0]') |
| 150 | + expect(context.context).toBe('c_test-name.property[0]') |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments