|
| 1 | +/** |
| 2 | + * Test file to demonstrate that the protobuf-utils wrapper handles unknown fields gracefully. |
| 3 | + * |
| 4 | + * This test can be run manually to verify the fix. Since the project doesn't have |
| 5 | + * a test runner configured, this serves as documentation of the expected behavior. |
| 6 | + * |
| 7 | + * To test manually: |
| 8 | + * 1. Add a new field to a protobuf schema on the backend |
| 9 | + * 2. Deploy the backend |
| 10 | + * 3. Use an older version of the webapp (without regenerating protobuf files) |
| 11 | + * 4. Verify that the webapp doesn't crash when receiving the new field |
| 12 | + */ |
| 13 | + |
| 14 | +import { fromJson } from "./protobuf-utils"; |
| 15 | +import { MessageSchema } from "../pkg/gen/apiclient/chat/v2/chat_pb"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Example: Testing that fromJson ignores unknown fields |
| 19 | + * |
| 20 | + * This would simulate a backend returning a message with a new field |
| 21 | + * that doesn't exist in the current schema. |
| 22 | + */ |
| 23 | +function testIgnoreUnknownFields() { |
| 24 | + // Simulate JSON response from backend with an extra field "newField" |
| 25 | + const jsonWithUnknownField = { |
| 26 | + messageId: "test-123", |
| 27 | + payload: { |
| 28 | + user: { |
| 29 | + content: "Hello", |
| 30 | + selectedText: "", |
| 31 | + newFieldThatDoesntExistYet: "This is a new field from a newer backend version", |
| 32 | + }, |
| 33 | + }, |
| 34 | + timestamp: "0", |
| 35 | + }; |
| 36 | + |
| 37 | + try { |
| 38 | + // This should NOT throw an error even though "newFieldThatDoesntExistYet" doesn't exist in the schema |
| 39 | + const message = fromJson(MessageSchema, jsonWithUnknownField); |
| 40 | + console.log("✓ Successfully parsed message with unknown field"); |
| 41 | + console.log(" Message ID:", message.messageId); |
| 42 | + console.log(" User content:", message.payload.user?.content); |
| 43 | + return true; |
| 44 | + } catch (error) { |
| 45 | + console.error("✗ Failed to parse message with unknown field:", error); |
| 46 | + return false; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Example: Testing that fromJson still validates required fields |
| 52 | + */ |
| 53 | +function testRequiredFieldsStillValidated() { |
| 54 | + // Missing required messageId field |
| 55 | + const invalidJson = { |
| 56 | + payload: { |
| 57 | + user: { |
| 58 | + content: "Hello", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + try { |
| 64 | + const message = fromJson(MessageSchema, invalidJson); |
| 65 | + console.log("✓ Parsed message (messageId will be empty string):", message.messageId); |
| 66 | + return true; |
| 67 | + } catch (error) { |
| 68 | + console.error("✗ Failed to parse message:", error); |
| 69 | + return false; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Export test functions for manual testing |
| 74 | +export { testIgnoreUnknownFields, testRequiredFieldsStillValidated }; |
0 commit comments