UNPKG

@mintlify/previewing

Version:

Preview Mintlify docs locally

70 lines (69 loc) 3.48 kB
import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { seedPreviewFromShared } from '../util.js'; describe('seedPreviewFromShared', () => { let tempDir; let sharedMintPath; let mintPath; let versionPath; beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mint-seed-preview-')); sharedMintPath = path.join(tempDir, 'shared', 'mint'); mintPath = path.join(tempDir, '3000', 'mint'); versionPath = path.join(mintPath, 'mint-version.txt'); }); afterEach(() => { fs.rmSync(tempDir, { recursive: true, force: true }); }); const writeClient = (clientPath, version, extraFile) => { fs.mkdirSync(clientPath, { recursive: true }); fs.writeFileSync(path.join(clientPath, 'mint-version.txt'), version); if (extraFile) { fs.writeFileSync(path.join(clientPath, extraFile), extraFile); } }; it('does nothing when the current workspace is the shared workspace', () => { writeClient(sharedMintPath, '2.0.0'); seedPreviewFromShared({ sharedMintPath, mintPath: sharedMintPath, versionPath: path.join(sharedMintPath, 'mint-version.txt'), }); expect(fs.readdirSync(path.join(tempDir, 'shared'))).toEqual(['mint']); expect(fs.existsSync(mintPath)).toBe(false); }); it('does nothing when the shared workspace has no client', () => { seedPreviewFromShared({ sharedMintPath, mintPath, versionPath }); expect(fs.existsSync(mintPath)).toBe(false); }); it('does nothing when the port workspace already has the shared version', () => { writeClient(sharedMintPath, '2.0.0', 'shared-only.txt'); writeClient(mintPath, '2.0.0', 'port-only.txt'); seedPreviewFromShared({ sharedMintPath, mintPath, versionPath }); expect(fs.existsSync(path.join(mintPath, 'port-only.txt'))).toBe(true); expect(fs.existsSync(path.join(mintPath, 'shared-only.txt'))).toBe(false); }); it('copies the shared client when the port workspace is empty', () => { writeClient(sharedMintPath, '2.0.0', 'client.txt'); seedPreviewFromShared({ sharedMintPath, mintPath, versionPath }); expect(fs.readFileSync(versionPath, 'utf8')).toBe('2.0.0'); expect(fs.readFileSync(path.join(mintPath, 'client.txt'), 'utf8')).toBe('client.txt'); }); it('replaces a stale port workspace with the shared client', () => { writeClient(sharedMintPath, '2.0.0', 'new-client.txt'); writeClient(mintPath, '1.0.0', 'old-client.txt'); seedPreviewFromShared({ sharedMintPath, mintPath, versionPath }); expect(fs.readFileSync(versionPath, 'utf8')).toBe('2.0.0'); expect(fs.existsSync(path.join(mintPath, 'new-client.txt'))).toBe(true); expect(fs.existsSync(path.join(mintPath, 'old-client.txt'))).toBe(false); }); it('does not overwrite a newer port client with an older shared client', () => { writeClient(sharedMintPath, '2.0.0', 'shared-only.txt'); writeClient(mintPath, '3.0.0', 'port-only.txt'); seedPreviewFromShared({ sharedMintPath, mintPath, versionPath }); expect(fs.readFileSync(versionPath, 'utf8')).toBe('3.0.0'); expect(fs.existsSync(path.join(mintPath, 'port-only.txt'))).toBe(true); expect(fs.existsSync(path.join(mintPath, 'shared-only.txt'))).toBe(false); }); });