@palantir/pack.state.foundry 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,229 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import type { Document, DocumentSearchResponse } from "@osdk/foundry.pack";
18
+ import { Documents } from "@osdk/foundry.pack";
19
+ import type { PackAppInternal } from "@palantir/pack.core";
20
+ import type {
21
+ DocumentId,
22
+ DocumentMetadata,
23
+ DocumentSchema,
24
+ } from "@palantir/pack.document-schema.model-types";
25
+ import { Metadata } from "@palantir/pack.document-schema.model-types";
26
+ import { createDocRef, DocumentLoadStatus } from "@palantir/pack.state.core";
27
+ import type { FoundryEventService, SyncSession } from "@palantir/pack.state.foundry-event";
28
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
29
+ import type { MockProxy } from "vitest-mock-extended";
30
+ import { mock } from "vitest-mock-extended";
31
+ import type { FoundryDocumentService } from "../FoundryDocumentService.js";
32
+ import { internalCreateFoundryDocumentService } from "../FoundryDocumentService.js";
33
+
34
+ vi.mock("@osdk/foundry.pack", () => ({
35
+ Documents: {
36
+ get: vi.fn(),
37
+ search: vi.fn(),
38
+ },
39
+ }));
40
+
41
+ const mockAuthModule = {
42
+ onTokenChange: vi.fn(),
43
+ getToken: vi.fn().mockReturnValue("mock-token"),
44
+ };
45
+
46
+ const mockLogger = {
47
+ child: vi.fn(),
48
+ debug: vi.fn((..._args: unknown[]) => {}),
49
+ error: vi.fn((..._args: unknown[]) => {}),
50
+ info: vi.fn((..._args: unknown[]) => {}),
51
+ warn: vi.fn((..._args: unknown[]) => {}),
52
+ };
53
+
54
+ mockLogger.child.mockReturnValue(mockLogger);
55
+
56
+ const mockOsdkClient = {};
57
+
58
+ const mockApp = {
59
+ getModule: vi.fn().mockReturnValue(mockAuthModule),
60
+ config: {
61
+ logger: mockLogger,
62
+ osdkClient: mockOsdkClient,
63
+ remote: {
64
+ packWsPath: "/ws",
65
+ baseUrl: "https://test.example.com",
66
+ },
67
+ },
68
+ } as unknown as PackAppInternal;
69
+
70
+ const testSchema = {
71
+ [Metadata]: {
72
+ version: 1,
73
+ },
74
+ } as const satisfies DocumentSchema;
75
+
76
+ const WIRE_DOCUMENT_WITH_SECURITY: Document = {
77
+ id: "test-doc-security",
78
+ name: "Secure Document",
79
+ documentTypeName: "SecureType",
80
+ ontologyRid: "ri.ontology..test-ontology-rid",
81
+ createdBy: "user-1",
82
+ createdTime: "2025-01-01T00:00:00Z",
83
+ updatedBy: "user-2",
84
+ updatedTime: "2025-01-02T00:00:00Z",
85
+ security: {
86
+ mandatory: {
87
+ classification: ["SECRET"],
88
+ markings: ["MARKING-1", "MARKING-2"],
89
+ },
90
+ discretionary: {
91
+ owners: [{ type: "userId", userId: "owner-user-1" }],
92
+ editors: [{ type: "groupId", groupId: "editors-group-1" }],
93
+ viewers: [{ type: "all" }],
94
+ },
95
+ },
96
+ };
97
+
98
+ describe("Foundry Security Conversion", () => {
99
+ let mockEventService: MockProxy<FoundryEventService>;
100
+ let service: FoundryDocumentService;
101
+ let sessionCounter: number;
102
+
103
+ beforeEach(() => {
104
+ vi.useFakeTimers();
105
+ mockEventService = mock();
106
+ sessionCounter = 0;
107
+
108
+ vi.mocked(Documents.get).mockResolvedValue(WIRE_DOCUMENT_WITH_SECURITY);
109
+
110
+ mockEventService.startDocumentSync.mockImplementation((documentId, _yDoc, onStatusChange) => {
111
+ const session: SyncSession = {
112
+ clientId: `test-client-${++sessionCounter}`,
113
+ documentId,
114
+ };
115
+
116
+ void Promise.resolve().then(() => {
117
+ onStatusChange({
118
+ load: DocumentLoadStatus.LOADED,
119
+ });
120
+ });
121
+
122
+ return session;
123
+ });
124
+
125
+ mockEventService.stopDocumentSync.mockImplementation(() => {});
126
+
127
+ service = internalCreateFoundryDocumentService(mockApp, {}, mockEventService);
128
+ });
129
+
130
+ afterEach(() => {
131
+ vi.useRealTimers();
132
+ vi.mocked(Documents.get).mockClear();
133
+ vi.mocked(Documents.search).mockClear();
134
+ });
135
+
136
+ describe("searchDocuments", () => {
137
+ it("should convert wire security to local security", async () => {
138
+ const searchResponse: DocumentSearchResponse = {
139
+ data: [WIRE_DOCUMENT_WITH_SECURITY],
140
+ };
141
+ vi.mocked(Documents.search).mockResolvedValue(searchResponse);
142
+
143
+ const result = await service.searchDocuments("SecureType", testSchema);
144
+
145
+ expect(result.data).toHaveLength(1);
146
+ const doc = result.data[0];
147
+
148
+ expect(doc?.security).toEqual({
149
+ mandatory: {
150
+ classification: ["SECRET"],
151
+ markings: ["MARKING-1", "MARKING-2"],
152
+ },
153
+ discretionary: {
154
+ owners: [{ type: "userId", userId: "owner-user-1" }],
155
+ editors: [{ type: "groupId", groupId: "editors-group-1" }],
156
+ viewers: [{ type: "all" }],
157
+ },
158
+ });
159
+ });
160
+
161
+ it("should preserve ontologyRid from response", async () => {
162
+ const searchResponse: DocumentSearchResponse = {
163
+ data: [WIRE_DOCUMENT_WITH_SECURITY],
164
+ };
165
+ vi.mocked(Documents.search).mockResolvedValue(searchResponse);
166
+
167
+ const result = await service.searchDocuments("SecureType", testSchema);
168
+
169
+ expect(result.data[0]?.ontologyRid).toBe("ri.ontology..test-ontology-rid");
170
+ });
171
+ });
172
+
173
+ describe("onMetadataSubscriptionOpened", () => {
174
+ const unsubscribes: Array<() => void> = [];
175
+
176
+ afterEach(() => {
177
+ unsubscribes.forEach(fn => {
178
+ fn();
179
+ });
180
+ unsubscribes.length = 0;
181
+ });
182
+
183
+ it("should convert wire security to local security in metadata", async () => {
184
+ const docRef = createDocRef(mockApp, "test-doc-security" as DocumentId, testSchema);
185
+
186
+ const metadataUpdates: DocumentMetadata[] = [];
187
+
188
+ unsubscribes.push(
189
+ service.onMetadataChange(docRef, (_, metadata) => {
190
+ metadataUpdates.push(metadata);
191
+ }),
192
+ );
193
+
194
+ await vi.runAllTimersAsync();
195
+
196
+ expect(metadataUpdates).toHaveLength(1);
197
+ const metadata = metadataUpdates[0];
198
+
199
+ expect(metadata?.security).toEqual({
200
+ mandatory: {
201
+ classification: ["SECRET"],
202
+ markings: ["MARKING-1", "MARKING-2"],
203
+ },
204
+ discretionary: {
205
+ owners: [{ type: "userId", userId: "owner-user-1" }],
206
+ editors: [{ type: "groupId", groupId: "editors-group-1" }],
207
+ viewers: [{ type: "all" }],
208
+ },
209
+ });
210
+ });
211
+
212
+ it("should preserve ontologyRid in metadata", async () => {
213
+ const docRef = createDocRef(mockApp, "test-doc-security" as DocumentId, testSchema);
214
+
215
+ const metadataUpdates: DocumentMetadata[] = [];
216
+
217
+ unsubscribes.push(
218
+ service.onMetadataChange(docRef, (_, metadata) => {
219
+ metadataUpdates.push(metadata);
220
+ }),
221
+ );
222
+
223
+ await vi.runAllTimersAsync();
224
+
225
+ expect(metadataUpdates).toHaveLength(1);
226
+ expect(metadataUpdates[0]?.ontologyRid).toBe("ri.ontology..test-ontology-rid");
227
+ });
228
+ });
229
+ });
@@ -82,7 +82,23 @@ const mockDocument: Document = {
82
82
  id: "test-doc",
83
83
  name: "Test Document",
84
84
  documentTypeName: "TestType",
85
- } as Document;
85
+ ontologyRid: "ri.ontology..test-rid",
86
+ createdBy: "user-1",
87
+ createdTime: "2025-01-01T00:00:00Z",
88
+ updatedBy: "user-1",
89
+ updatedTime: "2025-01-01T00:00:00Z",
90
+ security: {
91
+ mandatory: {
92
+ classification: [],
93
+ markings: [],
94
+ },
95
+ discretionary: {
96
+ owners: [],
97
+ editors: [],
98
+ viewers: [],
99
+ },
100
+ },
101
+ };
86
102
 
87
103
  describe("Foundry Document Status Tracking", () => {
88
104
  let mockEventService: MockProxy<FoundryEventService>;