This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
freecodecamp-projects/7-quality-assurance/4-sudoku-solver/tests/2_functional-tests.js

198 lines
6.7 KiB
JavaScript
Raw Normal View History

const chai = require("chai");
const chaiHttp = require("chai-http");
const assert = chai.assert;
const server = require("../server");
import {
puzzlesAndSolutions,
invalidStrings,
unsolvableStrings,
wrongLengthStrings,
} from "../controllers/puzzle-strings";
chai.use(chaiHttp);
suite("Functional Tests", () => {
suite("Solving puzzles", () => {
test("Solve a puzzle with valid puzzle string: POST request to /api/solve", (done) => {
for (let i in puzzlesAndSolutions) {
chai
.request(server)
.post("/api/solve")
.send({ puzzle: puzzlesAndSolutions[i][0] })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.solution, puzzlesAndSolutions[i][1]);
});
}
2022-11-12 21:40:00 +00:00
done();
});
test("Solve a puzzle with missing puzzle string: POST request to /api/solve", (done) => {
2022-11-12 21:40:00 +00:00
chai
.request(server)
.post("/api/solve")
.send({})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Required field missing");
done();
});
});
test("Solve a puzzle with invalid characters: POST request to /api/solve", (done) => {
for (let i in invalidStrings) {
chai
.request(server)
.post("/api/solve")
.send({ puzzle: invalidStrings[i] })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Invalid characters in puzzle");
});
}
2022-11-12 21:40:00 +00:00
done();
});
test("Solve a puzzle with incorrect length: POST request to /api/solve", (done) => {
for (let i in wrongLengthStrings) {
chai
.request(server)
.post("/api/solve")
.send({ puzzle: wrongLengthStrings[i] })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(
res.body.error,
2022-11-12 21:40:00 +00:00
"Expected puzzle to be 81 characters long"
);
});
}
2022-11-12 21:40:00 +00:00
done();
});
test("Solve a puzzle that cannot be solved: POST request to /api/solve", (done) => {
for (let i in unsolvableStrings) {
chai
.request(server)
.post("/api/solve")
.send({ puzzle: unsolvableStrings[i] })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Puzzle cannot be solved");
});
}
2022-11-12 21:40:00 +00:00
done();
});
});
2022-11-12 21:40:00 +00:00
suite("Placement Checks", () => {
test("Check a puzzle placement with all fields: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0][0], coordinate: "A2", value: 3 })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.valid, true);
done();
});
});
test("Check a puzzle placement with single placement conflict: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0][0], coordinate: "A2", value: 4 })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.valid, false);
assert.include(res.body.conflict, "row");
assert.notInclude(res.body.conflict, "region");
assert.notInclude(res.body.conflict, "column");
done();
});
});
test("Check a puzzle placement with multiple placement conflicts: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0][0], coordinate: "A2", value: 1 })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.valid, false);
assert.include(res.body.conflict, "row");
assert.include(res.body.conflict, "region");
assert.notInclude(res.body.conflict, "column");
done();
});
});
test("Check a puzzle placement with all placement conflicts: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0][0], coordinate: "A2", value: 2 })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.valid, false);
assert.include(res.body.conflict, "row");
assert.include(res.body.conflict, "region");
assert.include(res.body.conflict, "column");
done();
});
});
test("Check a puzzle placement with missing required fields: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0][0], coordinate: "A2" })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Required field(s) missing");
done();
});
});
test("Check a puzzle placement with invalid characters: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: invalidStrings[0], coordinate: "A2", value: "3" })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Invalid characters in puzzle");
done();
});
});
test("Check a puzzle placement with incorrect length: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: wrongLengthStrings[0], coordinate: "A2", value: "3" })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(
res.body.error,
"Expected puzzle to be 81 characters long"
);
done();
});
});
test("Check a puzzle placement with invalid placement coordinate: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0], coordinate: "Z11", value: "3" })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Invalid coordinate");
done();
});
});
test("Check a puzzle placement with invalid placement value: POST request to /api/check", (done) => {
chai
.request(server)
.post("/api/check")
.send({ puzzle: puzzlesAndSolutions[0], coordinate: "A2", value: "22" })
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.body.error, "Invalid value");
done();
});
});
});
});