|
| 1 | +import { describe, it, expect } from '@jest/globals'; |
| 2 | +import { patternHasher } from './template-hasher'; |
| 3 | + |
| 4 | +describe('patternHasher', () => { |
| 5 | + it('hashes and restore are mathematically reproducible', () => { |
| 6 | + const originalUrl = '{{host}}.example.com'; |
| 7 | + const { hashed, restore } = patternHasher(originalUrl); |
| 8 | + expect(hashed).toMatchInlineSnapshot(`"bruno-var-hash--163450413.example.com"`); |
| 9 | + expect(restore(hashed)).toEqual(originalUrl); |
| 10 | + }); |
| 11 | + |
| 12 | + it('hashes more than once', () => { |
| 13 | + const originalUrl = '{{host}}.example.{{new}}'; |
| 14 | + const { hashed, restore } = patternHasher(originalUrl); |
| 15 | + expect(hashed).toMatchInlineSnapshot(`"bruno-var-hash--163450413.example.bruno-var-hash-652560383"`); |
| 16 | + expect(restore(hashed)).toEqual(originalUrl); |
| 17 | + }); |
| 18 | + |
| 19 | + it('allows custom matchers', () => { |
| 20 | + const originalUrl = '$name.example.com'; |
| 21 | + const { hashed, restore } = patternHasher(originalUrl, /\$(\w+)/); |
| 22 | + expect(hashed).toMatchInlineSnapshot(`"bruno-var-hash-180907786.example.com"`); |
| 23 | + expect(restore(hashed)).toEqual(originalUrl); |
| 24 | + }); |
| 25 | + |
| 26 | + it('ignore unless matched', () => { |
| 27 | + const originalUrl = '$name.example.com'; |
| 28 | + const { hashed, restore } = patternHasher(originalUrl); |
| 29 | + expect(hashed).toMatchInlineSnapshot(`"$name.example.com"`); |
| 30 | + expect(restore(hashed)).toEqual(originalUrl); |
| 31 | + }); |
| 32 | +}); |
0 commit comments