Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'import { APP_API_VERSION, REAMS_VERSION, compareSemver, isVersionCompatible, resetVersionWarnings,} from '../version'
describe('version', () => { describe('REAMS_VERSION', () => { it('is a valid semver string', () => { expect(REAMS_VERSION).toMatch(/^\d+\.\d+\.\d+$/) })
it('matches package.json version', () => { // REAMS_VERSION should be the actual Reams version, not a fake Obsidian version expect(REAMS_VERSION).toBe('0.0.0') })
it('is exported from the reams module', async () => { // REAMS_VERSION is the canonical export from the 'reams' namespace module // eslint-disable-next-line @typescript-eslint/no-require-imports const reamsModule = await import('../../reams/index') expect(reamsModule.REAMS_VERSION).toBe(REAMS_VERSION) }) })
describe('APP_API_VERSION', () => { it('is a valid semver string', () => { expect(APP_API_VERSION).toMatch(/^\d+\.\d+\.\d+$/) })
it('equals REAMS_VERSION (truthful, not a lie)', () => { // APP_API_VERSION is now truthful - it equals the actual Reams version expect(APP_API_VERSION).toBe(REAMS_VERSION) })
it('is NOT a fake Obsidian version', () => { // The old lie was '1.11.4' - we must not pretend to be Obsidian expect(APP_API_VERSION).not.toBe('1.11.4') expect(APP_API_VERSION).not.toMatch(/^1\.\d+\.\d+$/) }) })
describe('compareSemver', () => { it('returns 0 for equal versions', () => { expect(compareSemver('1.0.0', '1.0.0')).toBe(0) expect(compareSemver('0.1.0', '0.1.0')).toBe(0) })
it('returns -1 when first is less than second', () => { expect(compareSemver('0.1.0', '1.0.0')).toBe(-1) expect(compareSemver('1.0.0', '1.1.0')).toBe(-1) expect(compareSemver('1.0.0', '1.0.1')).toBe(-1) })
it('returns 1 when first is greater than second', () => { expect(compareSemver('2.0.0', '1.0.0')).toBe(1) expect(compareSemver('1.2.0', '1.1.0')).toBe(1) expect(compareSemver('1.0.2', '1.0.1')).toBe(1) })
it('compares major before minor before patch', () => { expect(compareSemver('2.0.0', '1.9.9')).toBe(1) expect(compareSemver('1.2.0', '1.1.9')).toBe(1) })
it('handles partial versions', () => { expect(compareSemver('1', '1.0.0')).toBe(0) expect(compareSemver('1.2', '1.2.0')).toBe(0) })
it('strips prerelease suffixes', () => { // Prerelease versions compare equal to their base version expect(compareSemver('1.0.0-beta.1', '1.0.0')).toBe(0) expect(compareSemver('1.0.0', '1.0.0-alpha')).toBe(0) expect(compareSemver('2.0.0-rc.1', '1.9.9')).toBe(1) })
it('handles malformed input gracefully', () => { // Non-numeric parts are treated as 0 expect(compareSemver('garbage', '0.0.0')).toBe(0) expect(compareSemver('', '0.0.0')).toBe(0) expect(compareSemver('1.x.3', '1.0.3')).toBe(0) }) })
describe('isVersionCompatible', () => { let consoleWarnSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => { // Clear the warned versions set to ensure each test starts fresh resetVersionWarnings() consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) })
afterEach(() => { consoleWarnSpy.mockRestore() })
it('returns compatible=true for versions <= internal compat target', () => { // Compatibility is based on the internal OBSIDIAN_COMPAT_TARGET, not REAMS_VERSION // This allows real Obsidian plugins to load expect(isVersionCompatible('0.0.0').compatible).toBe(true) expect(isVersionCompatible('1.0.0').compatible).toBe(true) expect(isVersionCompatible('1.6.0').compatible).toBe(true) expect(isVersionCompatible('1.11.4').compatible).toBe(true) // The internal target })
it('returns compatible=false for versions > internal compat target', () => { expect(isVersionCompatible('999.0.0').compatible).toBe(false) expect(isVersionCompatible('2.0.0').compatible).toBe(false) // Boundary test: one patch above internal compat target should fail expect(isVersionCompatible('1.11.5').compatible).toBe(false) expect(isVersionCompatible('1.12.0').compatible).toBe(false) })
it('returns targetVersion for error messages', () => { // The result includes the target version for constructing error messages const result = isVersionCompatible('999.0.0') expect(result.targetVersion).toBe('1.11.4') })
it('accepts plugin requiring exact current Reams version', () => { expect(isVersionCompatible(REAMS_VERSION).compatible).toBe(true) })
it('warns when plugin targets version > REAMS_VERSION (once per version)', () => { // First call with '1.0.0' should warn isVersionCompatible('1.0.0') expect(consoleWarnSpy).toHaveBeenCalledOnce() expect(consoleWarnSpy).toHaveBeenCalledWith( expect.stringContaining('Plugin targets Obsidian') ) expect(consoleWarnSpy).toHaveBeenCalledWith( expect.stringContaining('running on Reams') )
// Second call with same version should NOT warn again (dedupe) consoleWarnSpy.mockClear() isVersionCompatible('1.0.0') expect(consoleWarnSpy).not.toHaveBeenCalled()
// Different version should warn isVersionCompatible('1.5.0') expect(consoleWarnSpy).toHaveBeenCalledOnce() })
it('does not warn for versions <= REAMS_VERSION', () => { isVersionCompatible('0.0.0').compatible // access to suppress lint expect(consoleWarnSpy).not.toHaveBeenCalled() })
it('warning message references PLUGIN-AUTHOR-GUIDE.md', () => { isVersionCompatible('0.9.0') // > REAMS_VERSION, warns expect(consoleWarnSpy).toHaveBeenCalledWith( expect.stringContaining('PLUGIN-AUTHOR-GUIDE.md') ) }) })})