|
| 1 | +import { describe, it } from "mocha"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { findCaseDiscrepancies, findLayoutNameOffset } from "../util/caseCheck"; |
| 4 | + |
| 5 | +describe("Test findLayoutNameOffset", () => { |
| 6 | + it("Finds offset for simple layout name", () => { |
| 7 | + const yaml = "layout:\n - name: template.cnfg\n source: wells"; |
| 8 | + const offset = findLayoutNameOffset(yaml, "template.cnfg"); |
| 9 | + expect(offset).to.equal(yaml.indexOf("template.cnfg")); |
| 10 | + }); |
| 11 | + |
| 12 | + it("Finds offset without leading dash", () => { |
| 13 | + const yaml = "layout:\n name: myfile.cnfg\n"; |
| 14 | + const offset = findLayoutNameOffset(yaml, "myfile.cnfg"); |
| 15 | + expect(offset).to.equal(yaml.indexOf("myfile.cnfg")); |
| 16 | + }); |
| 17 | + |
| 18 | + it("Returns -1 when name is not found", () => { |
| 19 | + const yaml = "layout:\n - name: other.cnfg\n"; |
| 20 | + const offset = findLayoutNameOffset(yaml, "missing.cnfg"); |
| 21 | + expect(offset).to.equal(-1); |
| 22 | + }); |
| 23 | + |
| 24 | + it("Handles special regex characters in filename", () => { |
| 25 | + const yaml = "layout:\n - name: file(1).cnfg\n"; |
| 26 | + const offset = findLayoutNameOffset(yaml, "file(1).cnfg"); |
| 27 | + expect(offset).to.equal(yaml.indexOf("file(1).cnfg")); |
| 28 | + }); |
| 29 | + |
| 30 | + it("Finds correct offset with multiple layout entries", () => { |
| 31 | + const yaml = "layout:\n - name: first.cnfg\n - name: second.cnfg\n"; |
| 32 | + const offset = findLayoutNameOffset(yaml, "second.cnfg"); |
| 33 | + expect(offset).to.equal(yaml.indexOf("second.cnfg")); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("Test findCaseDiscrepancies", () => { |
| 38 | + const yamlText = |
| 39 | + "layout:\n - name: Template.cnfg\n - name: correct.cnfg\n - name: Other.cnfg\n"; |
| 40 | + |
| 41 | + it("Detects case mismatch between layout name and directory entry", () => { |
| 42 | + const layoutNames = ["Template.cnfg"]; |
| 43 | + const dirEntries = ["template.cnfg"]; |
| 44 | + const result = findCaseDiscrepancies(layoutNames, dirEntries, yamlText); |
| 45 | + expect(result).to.have.lengthOf(1); |
| 46 | + expect(result[0].fileName).to.equal("Template.cnfg"); |
| 47 | + expect(result[0].actualName).to.equal("template.cnfg"); |
| 48 | + expect(result[0].offset).to.equal(yamlText.indexOf("Template.cnfg")); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Returns empty when casing matches exactly", () => { |
| 52 | + const layoutNames = ["correct.cnfg"]; |
| 53 | + const dirEntries = ["correct.cnfg"]; |
| 54 | + const result = findCaseDiscrepancies(layoutNames, dirEntries, yamlText); |
| 55 | + expect(result).to.have.lengthOf(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it("Returns empty when file does not exist in directory", () => { |
| 59 | + const layoutNames = ["nonexistent.cnfg"]; |
| 60 | + const dirEntries = ["template.cnfg", "correct.cnfg"]; |
| 61 | + const result = findCaseDiscrepancies(layoutNames, dirEntries, yamlText); |
| 62 | + expect(result).to.have.lengthOf(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it("Detects multiple case mismatches", () => { |
| 66 | + const layoutNames = ["Template.cnfg", "correct.cnfg", "Other.cnfg"]; |
| 67 | + const dirEntries = ["template.cnfg", "correct.cnfg", "other.cnfg"]; |
| 68 | + const result = findCaseDiscrepancies(layoutNames, dirEntries, yamlText); |
| 69 | + expect(result).to.have.lengthOf(2); |
| 70 | + expect(result[0].fileName).to.equal("Template.cnfg"); |
| 71 | + expect(result[0].actualName).to.equal("template.cnfg"); |
| 72 | + expect(result[1].fileName).to.equal("Other.cnfg"); |
| 73 | + expect(result[1].actualName).to.equal("other.cnfg"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("Returns empty for empty layout names", () => { |
| 77 | + const result = findCaseDiscrepancies([], ["file.cnfg"], yamlText); |
| 78 | + expect(result).to.have.lengthOf(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it("Returns empty for empty directory entries", () => { |
| 82 | + const result = findCaseDiscrepancies(["Template.cnfg"], [], yamlText); |
| 83 | + expect(result).to.have.lengthOf(0); |
| 84 | + }); |
| 85 | +}); |
0 commit comments