This commit is contained in:
JiriUhlir
2026-07-15 09:03:28 +02:00
parent 4b98f37407
commit fea25d7429
31 changed files with 4906 additions and 30 deletions
+106
View File
@@ -0,0 +1,106 @@
import axios, { AxiosAdapter, AxiosResponse } from "axios";
import { describe, expect, it } from "vitest";
import { SapBusinessOneServiceLayer } from "../src/SapBusinessOneServiceLayer";
import { SapB1Config } from "../src/config";
const config: SapB1Config = {
baseUrl: "https://sap.example.local:50000",
companyDB: "SBODEMOUS",
username: "manager",
password: "secret",
timeout: 30_000,
rejectUnauthorized: true,
retryCount: 0,
retryDelayMs: 0
};
function response(data: unknown, requestConfig: Parameters<AxiosAdapter>[0], status = 200): AxiosResponse {
return {
data,
status,
statusText: "OK",
headers: {},
config: requestConfig
};
}
describe("getSystemInfo", () => {
it("collects version, entity sets and customization metadata, isolating section errors", async () => {
const http = axios.create({
adapter: async (requestConfig) => {
const url = String(requestConfig.url);
if (url === "/Login") {
return {
...response({ SessionId: "abc123", Version: "1000230", SessionTimeout: 30 }, requestConfig),
headers: { "set-cookie": ["B1SESSION=abc123; Path=/b1s/v1"] }
};
}
if (url === "/Logout") {
return response({}, requestConfig);
}
if (url === "/") {
return response(
{ value: [{ name: "Items", url: "Items" }, { name: "BusinessPartners", url: "BusinessPartners" }] },
requestConfig
);
}
if (url === "UserFieldsMD") {
return response({ value: [{ TableName: "OCRD", FieldID: 0, Name: "MyField", Type: "db_Alpha" }] }, requestConfig);
}
if (url === "UserTablesMD") {
return response({ value: [{ TableName: "MY_TABLE", TableDescription: "Custom table" }] }, requestConfig);
}
if (url === "UserObjectsMD") {
return response({ value: [] }, requestConfig);
}
if (url === "CompanyService_GetAdminInfo") {
return Promise.reject({
isAxiosError: true,
message: "Forbidden",
response: { status: 403, data: { error: { message: { value: "No permission" } } } }
});
}
throw new Error(`Unexpected request URL: ${url}`);
}
});
const sap = new SapBusinessOneServiceLayer(config, { http });
const info = await sap.getSystemInfo();
expect(info.serviceLayer).toEqual({ version: "1000230", sessionTimeoutMinutes: 30 });
expect(info.entitySets).toEqual(["BusinessPartners", "Items"]);
expect(info.userFields).toEqual([{ TableName: "OCRD", FieldID: 0, Name: "MyField", Type: "db_Alpha" }]);
expect(info.userTables).toEqual([{ TableName: "MY_TABLE", TableDescription: "Custom table" }]);
expect(info.userObjects).toEqual([]);
expect(info.adminInfo).toEqual({ error: "No permission" });
});
it("keeps login info available after logout so the login endpoint can report the version", async () => {
const http = axios.create({
adapter: async (requestConfig) => {
if (requestConfig.url === "/Login") {
return {
...response({ SessionId: "abc123", Version: "1000230", SessionTimeout: 30 }, requestConfig),
headers: { "set-cookie": ["B1SESSION=abc123; Path=/b1s/v1"] }
};
}
return response({}, requestConfig);
}
});
const sap = new SapBusinessOneServiceLayer(config, { http });
await sap.login();
await sap.logout();
expect(sap.client.loginInfo).toEqual({ version: "1000230", sessionTimeoutMinutes: 30 });
});
});