ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.50: вынос resolveSessionOrganizationScopeContext(...) + related scope sanitation в отдельный runtime-adapter, чтобы добить верхний слой assistantService.
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
mergeFollowupContextWithOrganizationScopeRuntime,
|
||||
resolveSessionOrganizationScopeContextRuntime
|
||||
} from "../src/services/assistantOrganizationScopeRuntimeAdapter";
|
||||
|
||||
describe("assistant organization scope runtime adapter", () => {
|
||||
it("resolves selected organization from user message and promotes it to active scope", () => {
|
||||
const extractKnownOrganizationsFromHistory = vi.fn(() => ["Org A", "Org B"]);
|
||||
const resolveOrganizationSelectionFromMessage = vi.fn(() => "Org B");
|
||||
const findLastAssistantActiveOrganization = vi.fn(() => "Org A");
|
||||
const normalizeOrganizationScopeValue = vi.fn((value: unknown) =>
|
||||
typeof value === "string" && value.trim() ? value.trim() : null
|
||||
);
|
||||
|
||||
const context = resolveSessionOrganizationScopeContextRuntime({
|
||||
userMessage: "по Org B покажи",
|
||||
items: [] as any[],
|
||||
extractKnownOrganizationsFromHistory,
|
||||
resolveOrganizationSelectionFromMessage,
|
||||
findLastAssistantActiveOrganization,
|
||||
normalizeOrganizationScopeValue
|
||||
});
|
||||
|
||||
expect(context).toEqual({
|
||||
knownOrganizations: ["Org A", "Org B"],
|
||||
selectedOrganization: "Org B",
|
||||
activeOrganization: "Org B"
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back active organization to last assistant scope when selection is absent", () => {
|
||||
const normalizeOrganizationScopeValue = vi.fn((value: unknown) =>
|
||||
typeof value === "string" ? value.trim() : null
|
||||
);
|
||||
|
||||
const context = resolveSessionOrganizationScopeContextRuntime({
|
||||
userMessage: "просто обсуждаем",
|
||||
items: [] as any[],
|
||||
extractKnownOrganizationsFromHistory: () => ["Org A"],
|
||||
resolveOrganizationSelectionFromMessage: () => null,
|
||||
findLastAssistantActiveOrganization: () => "Org A",
|
||||
normalizeOrganizationScopeValue
|
||||
});
|
||||
|
||||
expect(context).toEqual({
|
||||
knownOrganizations: ["Org A"],
|
||||
selectedOrganization: null,
|
||||
activeOrganization: "Org A"
|
||||
});
|
||||
expect(normalizeOrganizationScopeValue).toHaveBeenCalledWith("Org A");
|
||||
});
|
||||
|
||||
it("merges organization into followup previous filters when organization is missing", () => {
|
||||
const merged = mergeFollowupContextWithOrganizationScopeRuntime({
|
||||
followupContext: {
|
||||
previous_filters: {
|
||||
period: "2020-07"
|
||||
}
|
||||
},
|
||||
organization: " Org A ",
|
||||
normalizeOrganizationScopeValue: (value: unknown) =>
|
||||
typeof value === "string" && value.trim() ? value.trim() : null,
|
||||
toNonEmptyString: (value: unknown) =>
|
||||
typeof value === "string" && value.trim().length > 0 ? value.trim() : null
|
||||
});
|
||||
|
||||
expect(merged).toEqual({
|
||||
previous_filters: {
|
||||
period: "2020-07",
|
||||
organization: "Org A"
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps existing organization in followup filters and returns null for empty context without org", () => {
|
||||
const preserved = mergeFollowupContextWithOrganizationScopeRuntime({
|
||||
followupContext: {
|
||||
previous_filters: {
|
||||
organization: "Org Existing"
|
||||
}
|
||||
},
|
||||
organization: "Org A",
|
||||
normalizeOrganizationScopeValue: (value: unknown) =>
|
||||
typeof value === "string" && value.trim() ? value.trim() : null,
|
||||
toNonEmptyString: (value: unknown) =>
|
||||
typeof value === "string" && value.trim().length > 0 ? value.trim() : null
|
||||
});
|
||||
const empty = mergeFollowupContextWithOrganizationScopeRuntime({
|
||||
followupContext: null,
|
||||
organization: "",
|
||||
normalizeOrganizationScopeValue: () => null,
|
||||
toNonEmptyString: () => null
|
||||
});
|
||||
|
||||
expect((preserved as any).previous_filters.organization).toBe("Org Existing");
|
||||
expect(empty).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user