136 lines
4.1 KiB
JavaScript
136 lines
4.1 KiB
JavaScript
// AI generated
|
|
|
|
// --- IMPORT YOUR ENGINE ---
|
|
// Since index.js uses default export class syntax, we import it directly
|
|
const GiteaEngine = require('../index.js').default;
|
|
|
|
// --- CONFIGURATION ---
|
|
const TEST_GITEA_URL = "https://gitea.furb.it"; // Change to your local instance if testing real connections
|
|
const API_PATH = "/api/v1/repos/search";
|
|
|
|
// Mock Settings (Replace with your config or local instance URL)
|
|
const settings = {
|
|
giteaUrl: TEST_GITEA_URL,
|
|
accessToken: ""
|
|
// Set accessToken to a real token if you want to test private repo search
|
|
};
|
|
|
|
// --- SETUP CONTEXT ---
|
|
// The degoog runtime provides these. We mock them for local testing.
|
|
const context = {
|
|
// Use global fetch (provided by node-fetch polyfill or native)
|
|
fetch: fetch,
|
|
sentinel: function(response, engineName) {
|
|
// In real degoog, this checks for network errors.
|
|
// We can log or ignore for local testing.
|
|
console.log(`[Sentinel] ${engineName} response status: ${response.status}`);
|
|
},
|
|
engineError: function(status, message, opts) {
|
|
// Create a generic error object to match the runtime expectation
|
|
const err = new Error(message);
|
|
err.name = "EngineError";
|
|
err.httpStatus = opts?.httpStatus || 500;
|
|
err.engine = opts?.engine || "Gitea";
|
|
return err;
|
|
}
|
|
};
|
|
|
|
// --- TEST EXECUTION ---
|
|
|
|
async function runConfigTest() {
|
|
console.log("Initializing Gitea Engine...");
|
|
engine = new GiteaEngine();
|
|
|
|
//Mock settings
|
|
empty_settings = {
|
|
giteaUrl: "",
|
|
accessToken: ""
|
|
};
|
|
|
|
incorrect_settings = {
|
|
giteaUrl: "this_is_not_a_url",
|
|
accessToken: ""
|
|
};
|
|
|
|
correct_settings = {
|
|
giteaUrl: TEST_GITEA_URL,
|
|
accessToken: ""
|
|
// Set accessToken to a real token if you want to test private repo search
|
|
};
|
|
|
|
// No config
|
|
configured = await engine.isConfigured();
|
|
if(configured){
|
|
console.error("Engine is not yet configured, but claims to be");
|
|
}
|
|
|
|
// Empty config
|
|
console.log("Configuring Gitea Engine with empty url...");
|
|
engine.configure(empty_settings);
|
|
|
|
configured = await engine.isConfigured();
|
|
if(configured){
|
|
console.error("Engine is incorrectly configured, but claims to be correct");
|
|
}
|
|
|
|
// Incorrect config
|
|
console.log("Configuring Gitea Engine with incorrect url...");
|
|
engine.configure(incorrect_settings);
|
|
|
|
configured = await engine.isConfigured();
|
|
if(configured){
|
|
console.error("Engine is incorrectly configured, but claims to be correct");
|
|
}
|
|
|
|
// Correct config
|
|
console.log("Configuring Gitea Engine with correct settings...");
|
|
engine.configure(correct_settings);
|
|
|
|
configured = await engine.isConfigured();
|
|
if(!configured){
|
|
console.error("Engine is correctly configured, but claims to be incorrect");
|
|
}
|
|
|
|
}
|
|
|
|
async function runSearchTest() {
|
|
console.log("Initializing Gitea Engine...");
|
|
engine = new GiteaEngine();
|
|
|
|
// 1. Configure the engine
|
|
engine.configure(settings);
|
|
console.log(`Configured Base URL: ${engine.giteaBase}`);
|
|
console.log(`Configured API Path: ${engine.giteaSearchUrl}`);
|
|
|
|
// 2. Run the search
|
|
// Note: For a local test without a real API, you would typically need a Mock Server
|
|
// Here we will attempt a call to the real hosted Gitea to verify logic.
|
|
// Replace 'query' with a term that exists on public gitea.com
|
|
const query = "esphome";
|
|
|
|
console.log(`\nExecuting search for: ${query}`);
|
|
|
|
try {
|
|
const results = await engine.executeSearch(
|
|
query,
|
|
1,
|
|
"",
|
|
context
|
|
);
|
|
|
|
console.log(`\n--- Search Results (${results.length} found) ---`);
|
|
results.forEach(r => {
|
|
console.log(`[${r.title}] -> ${r.url}`);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error("Search Failed:", error.message);
|
|
if (error.name === "SentinelBreach") {
|
|
console.log("Request was blocked or timed out.");
|
|
}
|
|
}
|
|
}
|
|
|
|
runConfigTest().catch(console.error);
|
|
runSearchTest().catch(console.error);
|