|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { expect } from 'chai'; |
| 5 | +import config from 'config'; |
| 6 | + |
| 7 | +describe('Collection', () => { |
| 8 | + const testCollectionPath = path.resolve(process.cwd(), config.get('@opentermsarchive/engine.collectionPath')); |
| 9 | + const metadataPath = path.join(testCollectionPath, 'metadata.yml'); |
| 10 | + const inventoryPath = path.join(testCollectionPath, 'deployment/inventory.yml'); |
| 11 | + let metadataBackup; |
| 12 | + let getCollection; |
| 13 | + let collection; |
| 14 | + |
| 15 | + before(async () => { |
| 16 | + try { |
| 17 | + metadataBackup = await fs.readFile(metadataPath, 'utf8'); |
| 18 | + } catch (error) { |
| 19 | + if (error.code !== 'ENOENT') throw error; |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + after(async () => { |
| 24 | + if (metadataBackup) { |
| 25 | + await fs.writeFile(metadataPath, metadataBackup); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + const { getCollection: reloadedGetCollection } = await import(`./index.js?t=${Date.now()}`); // Ensure a new instance is loaded for each test |
| 31 | + |
| 32 | + getCollection = reloadedGetCollection; |
| 33 | + }); |
| 34 | + |
| 35 | + describe('singleton behavior', () => { |
| 36 | + beforeEach(async () => { |
| 37 | + collection = await getCollection(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns same instance on multiple calls', async () => { |
| 41 | + const collection2 = await getCollection(); |
| 42 | + |
| 43 | + expect(collection).to.equal(collection2); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('metadata handling', () => { |
| 48 | + const metadata = { |
| 49 | + id: 'test-collection', |
| 50 | + name: 'Test Collection', |
| 51 | + tagline: 'Test collection for testing', |
| 52 | + }; |
| 53 | + |
| 54 | + describe('with existing metadata', () => { |
| 55 | + beforeEach(async () => { |
| 56 | + await fs.mkdir(testCollectionPath, { recursive: true }); |
| 57 | + await fs.writeFile(metadataPath, JSON.stringify(metadata)); |
| 58 | + collection = await getCollection(); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(async () => { |
| 62 | + await fs.rm(metadataPath, { recursive: true, force: true }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('loads collection id from metadata', () => { |
| 66 | + expect(collection.id).to.equal(metadata.id); |
| 67 | + }); |
| 68 | + |
| 69 | + it('loads collection name from metadata', () => { |
| 70 | + expect(collection.name).to.equal(metadata.name); |
| 71 | + }); |
| 72 | + |
| 73 | + it('loads full metadata object', () => { |
| 74 | + expect(collection.metadata).to.deep.equal(metadata); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('without metadata', () => { |
| 79 | + beforeEach(async () => { |
| 80 | + await fs.rm(metadataPath, { force: true }); |
| 81 | + collection = await getCollection(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('has undefined metadata', () => { |
| 85 | + expect(collection.metadata).to.be.undefined; |
| 86 | + }); |
| 87 | + |
| 88 | + it('has undefined id', () => { |
| 89 | + expect(collection.id).to.be.undefined; |
| 90 | + }); |
| 91 | + |
| 92 | + it('has undefined name', () => { |
| 93 | + expect(collection.name).to.be.undefined; |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('inventory handling', () => { |
| 99 | + const inventory = { |
| 100 | + all: { |
| 101 | + hosts: { |
| 102 | + 'test-host': { |
| 103 | + ansible_host: 'localhost', |
| 104 | + ansible_port: 22, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }; |
| 109 | + |
| 110 | + describe('with existing inventory', () => { |
| 111 | + beforeEach(async () => { |
| 112 | + await fs.mkdir(path.dirname(inventoryPath), { recursive: true }); |
| 113 | + await fs.writeFile(inventoryPath, JSON.stringify(inventory)); |
| 114 | + collection = await getCollection(); |
| 115 | + }); |
| 116 | + |
| 117 | + afterEach(async () => { |
| 118 | + await fs.rm(inventoryPath, { recursive: true, force: true }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('loads host from inventory', () => { |
| 122 | + expect(collection.host).to.equal('test-host'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('loads host config from inventory', () => { |
| 126 | + expect(collection.hostConfig).to.deep.equal(inventory.all.hosts['test-host']); |
| 127 | + }); |
| 128 | + |
| 129 | + it('loads full inventory object', () => { |
| 130 | + expect(collection.inventory).to.deep.equal(inventory); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('without inventory', () => { |
| 135 | + beforeEach(async () => { |
| 136 | + await fs.rm(inventoryPath, { force: true }); |
| 137 | + collection = await getCollection(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('has undefined inventory', () => { |
| 141 | + expect(collection.inventory).to.be.undefined; |
| 142 | + }); |
| 143 | + |
| 144 | + it('has undefined host', () => { |
| 145 | + expect(collection.host).to.be.undefined; |
| 146 | + }); |
| 147 | + |
| 148 | + it('has undefined host config', () => { |
| 149 | + expect(collection.hostConfig).to.be.undefined; |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments